From 656c2e2ce2a48605ad8020bacb93ea5707445e5a Mon Sep 17 00:00:00 2001 From: Finn <34718806+FinnDore@users.noreply.github.com> Date: Sun, 12 Nov 2023 16:28:30 +0000 Subject: [PATCH 1/2] works --- apps/client/src/app/page.tsx | 6 ++- apps/client/src/lazy-tauri-api/get-current.ts | 7 +++ apps/src-tauri/Cargo.lock | 49 +++++++++++++++++ apps/src-tauri/Cargo.toml | 1 + apps/src-tauri/src/main.rs | 54 +++++++++++++++++-- 5 files changed, 112 insertions(+), 5 deletions(-) diff --git a/apps/client/src/app/page.tsx b/apps/client/src/app/page.tsx index c3a2ae9..ef164f6 100644 --- a/apps/client/src/app/page.tsx +++ b/apps/client/src/app/page.tsx @@ -6,6 +6,7 @@ import type { NextPage } from 'next'; import clsx from 'clsx'; import { MacTrafficLights } from '~/components/traffic-lights'; +import { commands } from '~/lazy-tauri-api/get-current'; const Home: NextPage = () => { const [added, setAdded] = useState(true); @@ -18,7 +19,10 @@ const Home: NextPage = () => { setColor(e.target.value)} + onChange={e => { + setColor(e.target.value); + commands.set_color(e.target.value); + }} />
('set_color', { color }); + }, +}; diff --git a/apps/src-tauri/Cargo.lock b/apps/src-tauri/Cargo.lock index 5bd1f10..fd31207 100644 --- a/apps/src-tauri/Cargo.lock +++ b/apps/src-tauri/Cargo.lock @@ -1536,6 +1536,18 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mio" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +dependencies = [ + "libc", + "log", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys 0.48.0", +] + [[package]] name = "ndk" version = "0.6.0" @@ -2367,6 +2379,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "simd-adler32" version = "0.3.5" @@ -2394,6 +2415,16 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "socket2" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "soup2" version = "0.2.1" @@ -2817,6 +2848,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tokio", ] [[package]] @@ -2890,11 +2922,28 @@ checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f" dependencies = [ "autocfg", "bytes", + "libc", + "mio", "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "toml" version = "0.5.11" diff --git a/apps/src-tauri/Cargo.toml b/apps/src-tauri/Cargo.toml index 77f1d4e..719dbed 100644 --- a/apps/src-tauri/Cargo.toml +++ b/apps/src-tauri/Cargo.toml @@ -16,6 +16,7 @@ tauri-build = { version = "1.3", features = [] } tauri = { version = "1.3", features = ["macos-private-api", "shell-open", "window-close", "window-minimize", "window-set-fullscreen", "window-start-dragging"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +tokio = { version = "1", features = ["full"] } [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/apps/src-tauri/src/main.rs b/apps/src-tauri/src/main.rs index 523550d..fc66ff0 100644 --- a/apps/src-tauri/src/main.rs +++ b/apps/src-tauri/src/main.rs @@ -1,15 +1,61 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use std::sync::Arc; + +use tokio::io::AsyncWriteExt; + +use tokio::net::{TcpSocket, TcpStream}; +use tokio::sync::{Mutex, MutexGuard}; + +pub struct MutexState(Mutex); +struct InnerState { + pub b: u8, + pub tcp_stream: Option, + // pub tpc_socket: , +} + // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command #[tauri::command] -fn greet(name: &str) -> String { - format!("Hello, {}! You've been greeted from Rust!", name) +async fn set_color(color: &str, state: tauri::State<'_, MutexState>) -> Result<(), ()> { + println!("color: {}", color); + if let Some(inner) = state.0.lock().await.tcp_stream.as_mut() { + match inner.write_all(format!("{}00", color).as_bytes()).await { + Ok(_) => println!("ok"), + Err(e) => println!("error: {:?}", e), + } + } + // state + // .tcp_stream + // .as_mut() + // .unwrap() + // .write_all(b"#00110011") + // .await + // .unwrap(); + // state.b += 1; + // println!("b: {} {}", state.b, color); + Ok(()) } -fn main() { +#[tokio::main] +async fn main() { + let mut stream = TcpStream::connect("10.0.0.12:1234").await; + + let tcp_stream = match stream { + Ok(stream) => Some(stream), + Err(e) => { + println!("failed to connect to server: {:?}", e); + None + } + }; + // Write some data. + + // let socket = Some(socket); + + // let stream = socket.connect(addr).await?; tauri::Builder::default() - .invoke_handler(tauri::generate_handler![greet]) + .manage(MutexState(Mutex::new(InnerState { b: 0, tcp_stream }))) + .invoke_handler(tauri::generate_handler![set_color]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } From a1fef8c71360cf36db7ecbd357bd56c7449b76bf Mon Sep 17 00:00:00 2001 From: Finn <34718806+FinnDore@users.noreply.github.com> Date: Sun, 12 Nov 2023 20:12:53 +0000 Subject: [PATCH 2/2] frontend --- apps/client/src/app/page.tsx | 108 +++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/apps/client/src/app/page.tsx b/apps/client/src/app/page.tsx index ef164f6..f5ddf73 100644 --- a/apps/client/src/app/page.tsx +++ b/apps/client/src/app/page.tsx @@ -10,7 +10,11 @@ import { commands } from '~/lazy-tauri-api/get-current'; const Home: NextPage = () => { const [added, setAdded] = useState(true); - const [color, setColor] = useState('#5F00A9'); + const [color, setColor] = useState({ + hex: '#5F00A9', + rgb: { r: 95, g: 0, b: 169 }, + }); + return (
@@ -20,8 +24,11 @@ const Home: NextPage = () => { type="color" className="absolute" onChange={e => { - setColor(e.target.value); - commands.set_color(e.target.value); + setColor(() => ({ + hex: e.target.value, + rgb: hexToRgbString(e.target.value), + })); + void commands.set_color(e.target.value); }} /> @@ -44,7 +51,7 @@ export default Home; const AddLight = () => { return (
- +
Add Light
@@ -121,14 +128,19 @@ const AddLight = () => { ); }; -const Dots = (props: { purple?: boolean }) => ( -
+const Dots = (props: { purple?: boolean; hue?: number }) => ( +
@@ -149,13 +161,25 @@ const Dots = (props: { purple?: boolean }) => (
); -const Light = (props: { name: string; color: string }) => { +const Light = (props: { + name: string; + color: { + hex: string; + rgb: { + r: number; + g: number; + b: number; + }; + }; +}) => { return (
{props.name}
- +
{ style={{ background: 'conic-gradient(from 90deg at 50% 50%,' + - 'rgba(82, 0, 255, 0.00) 110.62499642372131deg,' + - 'rgba(143, 0, 255, 0.38) 206.25000715255737deg,' + - 'rgba(77, 0, 203, 0.30) 333.7499928474426deg)', + `rgba(${combineColor( + props.color.rgb.r, + 48, + )}, 0, ${combineColor( + props.color.rgb.b, + 86, + )}, 0.00) 110.62499642372131deg,` + + `rgba(${props.color.rgb.r + 143}, 0, ${combineColor( + props.color.rgb.b, + 86, + )}, 0.38) 206.25000715255737deg,` + + `rgba(${combineColor( + props.color.rgb.r, + -18, + )} , 0, ${combineColor( + props.color.rgb.b, + 34, + )}, 0.30) 333.7499928474426deg)`, }} >
{
{ style={{ background: 'conic-gradient(from 180deg at 50% 50%,' + - 'rgba(82, 0, 255, 0.00) 110.62499642372131deg,' + - '#8F00FF 206.25000715255737deg,' + - 'rgba(77, 0, 203, 0.79) 333.7499928474426deg)', + `rgba(${combineColor( + props.color.rgb.r, + 48, + )}, 0, ${combineColor( + props.color.rgb.b, + 86, + )}, 0.00) 110.62499642372131deg,` + + `rgba(${props.color.rgb.r + 143}, 0, ${combineColor( + props.color.rgb.b, + 86, + )}, 0.38) 206.25000715255737deg,` + + `rgba(${combineColor( + props.color.rgb.r, + -18, + )} , 0, ${combineColor( + props.color.rgb.b, + 34, + )}, 0.76) 333.7499928474426deg)`, }} >
{
); }; + +const combineColor = (color: number, color2: number) => { + const sum = color + color2; + if (sum > 255) { + return 255; + } else if (sum < 0) { + return 0; + } else { + return sum; + } +}; + +const hexToRgbString = (hex: string) => { + const bigint = parseInt(hex.replace('#', ''), 16); + const r = (bigint >> 16) & 255; + const g = (bigint >> 8) & 255; + const b = bigint & 255; + + return { r, g, b }; +};