11 Commits

Author SHA1 Message Date
3a44b96621 Update README.md with comprehensive project documentation
- Replace default Tauri template content with detailed project description
- Add comprehensive feature list and tech stack information
- Include complete installation and development setup guide
- Document application interface and configuration details
- Add development workflow and debugging tips
- Provide contributing guidelines and support information
- Write documentation in English for international accessibility
2025-07-04 21:57:36 +08:00
5da81e5f93 Fix resource leak and CPU performance issues
- Fix integer underflow panic in LED color publisher by adding bounds checking
- Reduce screenshot capture frequency from 15 FPS to 5 FPS for better CPU performance
- Reduce WebSocket force-send frequency from 200ms to 500ms
- Fix WebSocket resource leak by properly cleaning up streams when connections end
- Add proper stream lifecycle management with is_running flag checks
- Ensure background tasks exit when streams are stopped

This resolves the issue where CPU usage remained above 100% after visiting
the LED strip configuration page, even when navigating to other pages.
2025-07-04 21:49:05 +08:00
a10fae75d2 Refactor LED strip configuration interface layout
- Separate LED control panels from display preview areas
- Add dedicated LED count control section at bottom of page
- Create new LedCountControlPanel component with 4-column grid layout
- Fix display container height to prevent layout overflow
- Remove embedded LED controls from DisplayView component
- Improve text color for display info panel title
- Hide spinner buttons on number inputs for cleaner UI
- Enhance input field styling with centered text and larger font
2025-07-04 19:13:35 +08:00
5f12b8312a feat: enhance white balance interface with expandable help content
- Add comprehensive expandable help section in normal mode with detailed instructions
- Include usage recommendations, adjustment tips, and comparison methods
- Add simplified tooltip in fullscreen mode for quick reference
- Improve user guidance for LED strip color calibration process
- Maintain dual-mode functionality (normal/fullscreen) with appropriate help content
2025-07-04 18:31:44 +08:00
1944c88b55 Optimize screen streaming performance and clean up debug logs
- Reduced image processing time from 7-8 seconds to 340-420ms (15-20x improvement)
- Optimized BGRA->RGBA conversion with unsafe pointer operations and batch processing
- Changed image resize filter from Lanczos3 to Nearest for maximum speed
- Reduced target resolution from 400x225 to 320x180 for better performance
- Reduced JPEG quality from 75 to 50 for faster compression
- Fixed force-send mechanism timing from 500ms to 200ms intervals
- Improved frame rate from 0 FPS to ~2.5 FPS
- Cleaned up extensive debug logging and performance instrumentation
- Removed unused imports and variables to reduce compiler warnings
2025-07-04 14:45:50 +08:00
c8db28168c feat: Add Daisy-UI and optimize LED strip configuration UI
- Install and configure Tailwind CSS 4.1 with Daisy-UI plugin
- Redesign main navigation with responsive navbar and dark theme
- Optimize LED strip configuration layout with modern card components
- Improve screen preview performance with frame-based rendering
- Reduce LED pixel size for better visual appearance
- Remove excessive debug logging for better performance
- Fix Tailwind CSS ESM compatibility issues with dynamic imports
2025-07-03 13:28:19 +08:00
93ad9ae46c feat: implement real-time LED strip preview
- Add LED strip visualization around display previews
- Show real-time color status for each LED pixel
- Support multi-display LED strip configurations
- Use elegant 16px thin LED strip design
- Real-time LED color sync via WebSocket
- Responsive layout with display scaling support
2025-07-03 02:09:19 +08:00
6c30a824b0 feat: upgrade Tailwind CSS to v4.1.11
- Upgrade Tailwind CSS from 3.x to 4.x for latest features and performance
- Install @tailwindcss/postcss plugin for Tailwind CSS 4.0 compatibility
- Update CSS configuration to use new @import and @config syntax
- Update PostCSS configuration to use new plugin format
- Build working correctly with new Tailwind CSS engine
2025-06-30 18:01:26 +08:00
515b3a4ccb feat: upgrade Vite to v6.3.5
- Upgrade Vite from 4.x to 6.x for better performance and features
- Update @types/node to v24.0.7 for compatibility
- Maintain compatibility with vite-plugin-solid 2.11.7
- Build and development server working correctly
2025-06-30 17:51:53 +08:00
ddf61c861d feat: update dependencies to latest compatible versions
- Update frontend dependencies (SolidJS, Vite, Tailwind, etc.)
- Update backend dependencies (Tauri 1.8.3, Tokio, Serde, etc.)
- Fix thread safety issues with SafeDisplay wrapper for ddc-hi::Display
- Resolve display-info API compatibility issues
- All dependencies updated within major version constraints
2025-06-30 17:35:03 +08:00
b1fd751090 Fix LED color events and improve screenshot capture
- Fix LED color publisher: uncomment display_colors_tx.send() to enable LED color events
- Replace rust_swift_screencapture with screen-capture-kit for better macOS compatibility
- Add bounds checking in LED color processing to prevent array index errors
- Update screenshot manager to use CGDisplay as fallback implementation
- Fix frontend screenshot URL protocol to use ambient-light://
- Add debug logging for LED color events in frontend
- Remove debug logs that were added for troubleshooting
- Update dependencies and remove CMake-dependent paho-mqtt temporarily

This resolves the issue where LED color events were not being sent to the frontend,
enabling real-time LED color visualization in the UI.
2025-06-30 14:35:03 +08:00
60 changed files with 12089 additions and 3399 deletions

189
README.md
View File

@ -1,7 +1,188 @@
# Tauri + Solid + Typescript
# Display Ambient Light Desktop App
This template should help get you started developing with Tauri, Solid and Typescript in Vite.
A desktop application built with Tauri 2.0 for ambient light control, supporting multi-monitor screen sampling and LED strip control to create immersive ambient lighting effects.
## Recommended IDE Setup
## ✨ Features
- [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)
- 🖥️ **Multi-Monitor Support** - Automatic detection and configuration of multiple displays
- 🎨 **Real-time Screen Sampling** - High-performance screen content capture and color analysis
- 💡 **LED Strip Control** - Configurable LED strip layout and mapping support
- ⚖️ **White Balance Calibration** - Built-in white balance adjustment tool with fullscreen mode
- 🎛️ **Intuitive Configuration Interface** - Modern UI with drag-and-drop configuration support
- 🔧 **Hardware Integration** - Display brightness control and audio device management
- 📡 **Network Communication** - UDP and WebSocket communication support
## 🛠️ Tech Stack
### Frontend
- **Framework**: [Solid.js](https://solidjs.com/) - High-performance reactive UI framework
- **Build Tool**: [Vite](https://vitejs.dev/) - Fast frontend build tool
- **Styling**: [Tailwind CSS](https://tailwindcss.com/) + [DaisyUI](https://daisyui.com/) - Modern UI component library
- **Routing**: [@solidjs/router](https://github.com/solidjs/solid-router) - Client-side routing
- **Language**: TypeScript - Type-safe JavaScript
### Backend
- **Framework**: [Tauri 2.0](https://tauri.app/) - Cross-platform desktop app framework
- **Language**: Rust - High-performance systems programming language
- **Screen Capture**: [screen-capture-kit](https://crates.io/crates/screen-capture-kit) - macOS native screen capture
- **Display Control**: [ddc-hi](https://crates.io/crates/ddc-hi) - DDC/CI display control
- **Audio**: [coreaudio-rs](https://crates.io/crates/coreaudio-rs) - macOS audio system integration
- **Networking**: tokio + tokio-tungstenite - Async network communication
## 📋 System Requirements
- **Operating System**: macOS 13.0+ (primary supported platform)
- **Memory**: 4GB+ recommended
- **Graphics**: Hardware-accelerated graphics card
- **Network**: For device discovery and communication
## 🚀 Quick Start
### Prerequisites
1. **Install Rust**
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
```
2. **Install Node.js and pnpm**
```bash
# Install Node.js (recommended using nvm)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install node
# Install pnpm
npm install -g pnpm
```
3. **Install Tauri CLI**
```bash
cargo install @tauri-apps/cli@next
```
### Development Setup
1. **Clone the project**
```bash
git clone <repository-url>
cd display-ambient-light/desktop
```
2. **Install dependencies**
```bash
pnpm install
```
3. **Start development server**
```bash
pnpm tauri dev
```
### Production Build
```bash
# Build the application
pnpm tauri build
# Build artifacts are located in src-tauri/target/release/bundle/
```
## 📱 Application Interface
### Main Pages
1. **System Info** (`/info`) - Display system and hardware information
2. **Display Info** (`/displays`) - Monitor status and configuration
3. **LED Strip Configuration** (`/led-strips-configuration`) - LED strip layout and mapping configuration
4. **White Balance** (`/white-balance`) - Color calibration and white balance adjustment
### Core Features
- **Real-time Screen Preview** - WebSocket streaming of screen content
- **LED Mapping Configuration** - Visual configuration of LED strip positions and quantities
- **Color Calibration** - RGB adjustment panel with fullscreen comparison mode
- **Device Management** - Automatic discovery and management of LED control devices
## 🔧 Configuration Files
Application configuration is stored in the user directory:
```text
~/Library/Application Support/cc.ivanli.ambient-light.desktop/
├── config.toml # Main configuration file
├── led_strips.json # LED strip configuration
└── color_calibration.json # Color calibration data
```
## 🎯 Development Guide
### Project Structure
```text
desktop/
├── src/ # Frontend source code (Solid.js)
│ ├── components/ # UI components
│ ├── stores/ # State management
│ ├── models/ # Data models
│ └── contexts/ # React Context
├── src-tauri/ # Backend source code (Rust)
│ ├── src/
│ │ ├── ambient_light/ # Ambient light control
│ │ ├── display/ # Display management
│ │ ├── rpc/ # Network communication
│ │ └── screenshot/ # Screen capture
│ └── tauri.conf.json # Tauri configuration
└── package.json # Frontend dependencies
```
### Development Workflow
1. **Frontend Development**: Modify files under `src/`, supports hot reload
2. **Backend Development**: Modify files under `src-tauri/src/`, requires dev server restart
3. **Configuration Changes**: Restart required after modifying `tauri.conf.json`
### Debugging Tips
- Use browser developer tools to debug frontend
- Use `console.log` and Rust's `println!` for debugging
- Check Tauri console output for backend logs
## 🤝 Contributing
1. Fork the project
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
## 📄 License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## 🔗 Related Links
- [Tauri Official Documentation](https://tauri.app/)
- [Solid.js Official Documentation](https://solidjs.com/)
- [Rust Official Documentation](https://doc.rust-lang.org/)
- [Tailwind CSS Documentation](https://tailwindcss.com/docs)
## 📞 Support
If you encounter issues or have suggestions, please:
- Create an [Issue](../../issues)
- Check the [Wiki](../../wiki) for more information
- Contact the developer
---
**Note**: This application is primarily optimized for macOS platform, support for other platforms may be limited.

16
debug_displays.rs Normal file
View File

@ -0,0 +1,16 @@
use display_info;
fn main() {
match display_info::DisplayInfo::all() {
Ok(displays) => {
println!("Found {} displays:", displays.len());
for (index, display) in displays.iter().enumerate() {
println!(" Display {}: ID={}, Scale={}, Width={}, Height={}",
index, display.id, display.scale_factor, display.width, display.height);
}
}
Err(e) => {
println!("Error getting display info: {}", e);
}
}
}

View File

@ -11,23 +11,26 @@
},
"license": "MIT",
"dependencies": {
"@solidjs/router": "^0.8.2",
"@tauri-apps/api": "^1.3.0",
"debug": "^4.3.4",
"solid-icons": "^1.0.8",
"solid-js": "^1.7.6",
"@solidjs/router": "^0.8.4",
"@tauri-apps/api": "^2.6.0",
"debug": "^4.4.1",
"solid-icons": "^1.1.0",
"solid-js": "^1.9.7",
"solid-tippy": "^0.2.1",
"tippy.js": "^6.3.7"
},
"devDependencies": {
"@tauri-apps/cli": "^1.3.1",
"@types/debug": "^4.1.8",
"@types/node": "^18.16.17",
"autoprefixer": "^10.4.14",
"postcss": "^8.4.24",
"tailwindcss": "^3.3.2",
"@tailwindcss/postcss": "^4.1.11",
"@tailwindcss/vite": "^4.1.11",
"@tauri-apps/cli": "^2.6.2",
"@types/debug": "^4.1.12",
"@types/node": "^24.0.7",
"autoprefixer": "^10.4.21",
"daisyui": "^5.0.43",
"postcss": "^8.5.6",
"tailwindcss": "^4.1.11",
"typescript": "^4.9.5",
"vite": "^4.3.9",
"vite-plugin-solid": "^2.7.0"
"vite": "^6.3.5",
"vite-plugin-solid": "^2.11.7"
}
}

2817
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,5 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}

4450
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,11 +10,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
tauri-build = { version = "1.2", features = [] }
tauri-build = { version = "2.0", features = [] }
[dependencies]
tauri = { version = "1.2", features = ["shell-open"] }
tauri = { version = "2.0", features = ["tray-icon"] }
tauri-plugin-shell = "2.0"
serde = { version = "1.0", features = ["derive"] }
dirs = "5.0"
regex = "1.0"
serde_json = "1.0"
core-graphics = "0.22.3"
display-info = "0.4.1"
@ -28,8 +31,8 @@ url-build-parse = "9.0.0"
color_space = "0.5.3"
hex = "0.4.3"
toml = "0.7.3"
paho-mqtt = "0.12.1"
time = {version="0.3.20", features= ["formatting"] }
# paho-mqtt = "0.12.1" # Temporarily disabled due to CMake issues
time = {version="0.3.35", features= ["formatting"] }
itertools = "0.10.5"
core-foundation = "0.9.3"
tokio-stream = "0.1.14"
@ -37,7 +40,11 @@ mdns-sd = "0.7.2"
futures = "0.3.28"
ddc-hi = "0.4.1"
coreaudio-rs = "0.11.2"
rust_swift_screencapture = { version = "0.1.1", path = "../../../../demo/rust-swift-screencapture" }
screen-capture-kit = "0.3.1"
image = { version = "0.24", features = ["jpeg"] }
tokio-tungstenite = "0.20"
futures-util = "0.3"
sha1 = "0.10"
[features]
# this feature is used for production builds or when `devPath` points to the filesystem

View File

@ -0,0 +1,12 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main application window",
"windows": ["main"],
"permissions": [
"core:default",
"shell:allow-open",
"core:window:allow-set-fullscreen",
"core:window:allow-is-fullscreen"
]
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
{"default":{"identifier":"default","description":"Capability for the main application window","local":true,"windows":["main"],"permissions":["core:default","shell:allow-open","core:window:allow-set-fullscreen","core:window:allow-is-fullscreen"]}}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

3
src-tauri/src-tauri/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Generated by Cargo
# will have compiled files and executables
/target/

View File

@ -0,0 +1,26 @@
[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
edition = "2021"
rust-version = "1.60"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
tauri-build = { version = "1.5.6" }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.8.2" }
[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
# If you use cargo directly instead of tauri's cli you can use this feature flag to switch between tauri's `dev` and `build` modes.
# DO NOT REMOVE!!
custom-protocol = [ "tauri/custom-protocol" ]

View File

@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

View File

@ -0,0 +1,8 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
tauri::Builder::default()
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@ -0,0 +1,65 @@
{
"build": {
"beforeBuildCommand": "npm run build",
"beforeDevCommand": "npm run dev",
"devPath": "http://localhost:4000",
"distDir": "../dist"
},
"package": {
"productName": "Tauri App",
"version": "0.1.0"
},
"tauri": {
"allowlist": {
"all": false
},
"bundle": {
"active": true,
"category": "DeveloperTool",
"copyright": "",
"deb": {
"depends": []
},
"externalBin": [],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "com.tauri.dev",
"longDescription": "",
"macOS": {
"entitlements": null,
"exceptionDomain": "",
"frameworks": [],
"providerShortName": null,
"signingIdentity": null
},
"resources": [],
"shortDescription": "",
"targets": "all",
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
},
"security": {
"csp": null
},
"updater": {
"active": false
},
"windows": [
{
"fullscreen": false,
"height": 600,
"resizable": true,
"title": "Tauri",
"width": 800
}
]
}
}

View File

@ -3,7 +3,6 @@ use std::env::current_dir;
use display_info::DisplayInfo;
use paris::{error, info};
use serde::{Deserialize, Serialize};
use tauri::api::path::config_dir;
use crate::screenshot::LedSamplePoints;
@ -55,7 +54,7 @@ impl LedStripConfigGroup {
let displays = DisplayInfo::all()?;
// config path
let path = config_dir()
let path = dirs::config_dir()
.unwrap_or(current_dir().unwrap())
.join(CONFIG_FILE_NAME);
@ -83,7 +82,7 @@ impl LedStripConfigGroup {
}
pub async fn write_config(configs: &Self) -> anyhow::Result<()> {
let path = config_dir()
let path = dirs::config_dir()
.unwrap_or(current_dir().unwrap())
.join(CONFIG_FILE_NAME);

View File

@ -88,21 +88,21 @@ impl LedColorsPublisher {
}
}
// match display_colors_tx.send((
// display_id,
// colors_copy
// .into_iter()
// .map(|color| color.get_rgb())
// .flatten()
// .collect::<Vec<_>>(),
// )) {
// Ok(_) => {
// // log::info!("sent colors: {:?}", color_len);
// }
// Err(err) => {
// warn!("Failed to send display_colors: {}", err);
// }
// };
match display_colors_tx.send((
display_id,
colors_copy
.into_iter()
.map(|color| color.get_rgb())
.flatten()
.collect::<Vec<_>>(),
)) {
Ok(_) => {
// log::info!("sent colors: {:?}", color_len);
}
Err(err) => {
warn!("Failed to send display_colors: {}", err);
}
};
// Check if the inner task version changed
let version = internal_tasks_version.read().await.clone();
@ -127,7 +127,7 @@ impl LedColorsPublisher {
) {
let sorted_colors_tx = self.sorted_colors_tx.clone();
let colors_tx = self.colors_tx.clone();
log::debug!("start all_colors_worker");
tokio::spawn(async move {
for _ in 0..10 {
@ -137,7 +137,7 @@ impl LedColorsPublisher {
let mut all_colors: Vec<Option<Vec<u8>>> = vec![None; display_ids.len()];
let mut start: tokio::time::Instant = tokio::time::Instant::now();
log::debug!("start all_colors_worker task");
loop {
let color_info = display_colors_rx.recv().await;
@ -186,7 +186,7 @@ impl LedColorsPublisher {
warn!("Failed to send sorted colors: {}", err);
}
};
log::debug!("tick: {}ms", start.elapsed().as_millis());
start = tokio::time::Instant::now();
}
}
@ -195,7 +195,7 @@ impl LedColorsPublisher {
}
pub async fn start(&self) {
log::info!("start colors worker");
let config_manager = ConfigManager::global().await;
let mut config_receiver = config_manager.clone_config_update_receiver();
@ -203,9 +203,7 @@ impl LedColorsPublisher {
self.handle_config_change(configs).await;
log::info!("waiting for config update...");
while config_receiver.changed().await.is_ok() {
log::info!("config updated, restart inner tasks...");
let configs = config_receiver.borrow().clone();
self.handle_config_change(configs).await;
}
@ -298,18 +296,50 @@ impl LedColorsPublisher {
let mut buffer = Vec::<u8>::with_capacity(group_size * 3);
if group.end > group.start {
for i in group.pos - display_led_offset..group_size + group.pos - display_led_offset
{
let bytes = colors[i].as_bytes();
buffer.append(&mut bytes.to_vec());
// Prevent integer underflow by using saturating subtraction
let start_index = if group.pos >= display_led_offset {
group.pos - display_led_offset
} else {
0
};
let end_index = if group.pos + group_size >= display_led_offset {
group_size + group.pos - display_led_offset
} else {
0
};
for i in start_index..end_index {
if i < colors.len() {
let bytes = colors[i].as_bytes();
buffer.append(&mut bytes.to_vec());
} else {
log::warn!("Index {} out of bounds for colors array of length {}", i, colors.len());
// Add black color as fallback
buffer.append(&mut vec![0, 0, 0]);
}
}
} else {
for i in (group.pos - display_led_offset
..group_size + group.pos - display_led_offset)
.rev()
{
let bytes = colors[i].as_bytes();
buffer.append(&mut bytes.to_vec());
// Prevent integer underflow by using saturating subtraction
let start_index = if group.pos >= display_led_offset {
group.pos - display_led_offset
} else {
0
};
let end_index = if group.pos + group_size >= display_led_offset {
group_size + group.pos - display_led_offset
} else {
0
};
for i in (start_index..end_index).rev() {
if i < colors.len() {
let bytes = colors[i].as_bytes();
buffer.append(&mut bytes.to_vec());
} else {
log::warn!("Index {} out of bounds for colors array of length {}", i, colors.len());
// Add black color as fallback
buffer.append(&mut vec![0, 0, 0]);
}
}
}
@ -350,7 +380,7 @@ impl LedColorsPublisher {
let mut screenshots = HashMap::new();
loop {
log::info!("waiting merged screenshot...");
let screenshot = merged_screenshot_receiver.recv().await;
if let Err(err) = screenshot {
@ -382,7 +412,7 @@ impl LedColorsPublisher {
.filter(|(_, c)| c.display_id == display_id);
let screenshot = screenshots.get(&display_id).unwrap();
log::debug!("screenshot updated: {:?}", display_id);
let points: Vec<_> = led_strip_configs
.clone()
@ -412,7 +442,7 @@ impl LedColorsPublisher {
led_start = led_end;
}
log::debug!("got all colors configs: {:?}", colors_configs.len());
return Ok(AllColorConfig {
sample_point_groups: colors_configs,

View File

@ -5,9 +5,27 @@ use tokio::sync::RwLock;
use super::DisplayState;
// Safe wrapper for Display that implements Send + Sync
pub struct SafeDisplay {
display: Display,
}
unsafe impl Send for SafeDisplay {}
unsafe impl Sync for SafeDisplay {}
impl SafeDisplay {
pub fn new(display: Display) -> Self {
Self { display }
}
pub fn get_mut(&mut self) -> &mut Display {
&mut self.display
}
}
pub struct DisplayHandler {
pub state: Arc<RwLock<DisplayState>>,
pub controller: Arc<RwLock<Display>>,
pub controller: Arc<RwLock<SafeDisplay>>,
}
impl DisplayHandler {
@ -16,7 +34,7 @@ impl DisplayHandler {
let mut temp_state = self.state.read().await.clone();
match controller.handle.get_vcp_feature(0x10) {
match controller.get_mut().handle.get_vcp_feature(0x10) {
Ok(value) => {
temp_state.max_brightness = value.maximum();
temp_state.min_brightness = 0;
@ -24,7 +42,7 @@ impl DisplayHandler {
}
Err(_) => {}
};
match controller.handle.get_vcp_feature(0x12) {
match controller.get_mut().handle.get_vcp_feature(0x12) {
Ok(value) => {
temp_state.max_contrast = value.maximum();
temp_state.min_contrast = 0;
@ -32,7 +50,7 @@ impl DisplayHandler {
}
Err(_) => {}
};
match controller.handle.get_vcp_feature(0xdc) {
match controller.get_mut().handle.get_vcp_feature(0xdc) {
Ok(value) => {
temp_state.max_mode = value.maximum();
temp_state.min_mode = 0;
@ -52,6 +70,7 @@ impl DisplayHandler {
let mut state = self.state.write().await;
controller
.get_mut()
.handle
.set_vcp_feature(0x10, brightness)
.map_err(|err| anyhow::anyhow!("can not set brightness. {:?}", err))?;
@ -69,6 +88,7 @@ impl DisplayHandler {
let mut state = self.state.write().await;
controller
.get_mut()
.handle
.set_vcp_feature(0x12, contrast)
.map_err(|err| anyhow::anyhow!("can not set contrast. {:?}", err))?;
@ -84,6 +104,7 @@ impl DisplayHandler {
let mut state = self.state.write().await;
controller
.get_mut()
.handle
.set_vcp_feature(0xdc, mode)
.map_err(|err| anyhow::anyhow!("can not set mode. {:?}", err))?;

View File

@ -2,7 +2,7 @@ use std::{env::current_dir, sync::Arc, time::Duration};
use ddc_hi::Display;
use paris::{error, info, warn};
use tauri::api::path::config_dir;
use dirs::config_dir;
use tokio::{
sync::{broadcast, watch, OnceCell, RwLock},
task::yield_now,
@ -13,7 +13,7 @@ use crate::{
rpc::{BoardMessageChannels, DisplaySetting},
};
use super::{display_handler::DisplayHandler, display_state::DisplayState};
use super::{display_handler::{DisplayHandler, SafeDisplay}, display_state::DisplayState};
const CONFIG_FILE_NAME: &str = "cc.ivanli.ambient_light/displays.toml";
@ -85,7 +85,8 @@ impl DisplayManager {
let controllers = Display::enumerate();
for display in controllers {
let controller = Arc::new(RwLock::new(display));
let safe_display = SafeDisplay::new(display);
let controller = Arc::new(RwLock::new(safe_display));
let state = Arc::new(RwLock::new(DisplayState::default()));
let handler = DisplayHandler {
state: state.clone(),

View File

@ -7,6 +7,7 @@ mod led_color;
mod rpc;
mod screenshot;
mod screenshot_manager;
mod screen_stream;
mod volume;
use ambient_light::{Border, ColorCalibration, LedStripConfig, LedStripConfigGroup};
@ -16,11 +17,13 @@ use paris::{error, info, warn};
use rpc::{BoardInfo, UdpRpc};
use screenshot::Screenshot;
use screenshot_manager::ScreenshotManager;
use serde::{Deserialize, Serialize};
use serde_json::to_string;
use tauri::{http::ResponseBuilder, regex, Manager};
use tauri::{Manager, Emitter, Runtime};
use regex;
use tauri::http::{Request, Response};
use volume::VolumeManager;
#[derive(Serialize, Deserialize)]
#[serde(remote = "DisplayInfo")]
struct DisplayInfoDef {
@ -213,10 +216,142 @@ async fn get_displays() -> Vec<DisplayState> {
display_manager.get_displays().await
}
// Protocol handler for ambient-light://
fn handle_ambient_light_protocol<R: Runtime>(
_ctx: tauri::UriSchemeContext<R>,
request: Request<Vec<u8>>
) -> Response<Vec<u8>> {
let url = request.uri();
// info!("Handling ambient-light protocol request: {}", url);
// Parse the URL to extract parameters
let url_str = url.to_string();
let re = regex::Regex::new(r"ambient-light://displays/(\d+)\?width=(\d+)&height=(\d+)").unwrap();
if let Some(captures) = re.captures(&url_str) {
let display_id: u32 = captures[1].parse().unwrap_or(0);
let width: u32 = captures[2].parse().unwrap_or(400);
let height: u32 = captures[3].parse().unwrap_or(300);
// info!("Efficient screenshot request for display {}, {}x{}", display_id, width, height);
// Optimized screenshot processing with much smaller intermediate size
// info!("Screenshot request received: display_id={}, width={}, height={}", display_id, width, height);
let screenshot_data = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let screenshot_manager = ScreenshotManager::global().await;
let channels = screenshot_manager.channels.read().await;
if let Some(rx) = channels.get(&display_id) {
let rx = rx.read().await;
let screenshot = rx.borrow().clone();
let bytes = screenshot.bytes.read().await.to_owned();
// Use much smaller intermediate resolution for performance
let intermediate_width = 800; // Much smaller than original 5120
let intermediate_height = 450; // Much smaller than original 2880
// Convert BGRA to RGBA format
let mut rgba_bytes = bytes.as_ref().clone();
for chunk in rgba_bytes.chunks_exact_mut(4) {
chunk.swap(0, 2); // Swap B and R channels
}
let image_result = image::RgbaImage::from_raw(
screenshot.width as u32,
screenshot.height as u32,
rgba_bytes,
);
if let Some(img) = image_result {
// Step 1: Fast downscale to intermediate size
let intermediate_image = image::imageops::resize(
&img,
intermediate_width,
intermediate_height,
image::imageops::FilterType::Nearest, // Fastest possible
);
// Step 2: Scale to final target size
let final_image = if width == intermediate_width && height == intermediate_height {
intermediate_image
} else {
image::imageops::resize(
&intermediate_image,
width,
height,
image::imageops::FilterType::Triangle,
)
};
let raw_data = final_image.into_raw();
// info!("Efficient resize completed: {}x{}, {} bytes", width, height, raw_data.len());
Ok(raw_data)
} else {
error!("Failed to create image from raw bytes");
Err("Failed to create image from raw bytes".to_string())
}
} else {
error!("Display {} not found", display_id);
Err(format!("Display {} not found", display_id))
}
})
});
match screenshot_data {
Ok(data) => {
Response::builder()
.header("Content-Type", "application/octet-stream")
.header("Access-Control-Allow-Origin", "*")
.header("X-Image-Width", width.to_string())
.header("X-Image-Height", height.to_string())
.body(data)
.unwrap_or_else(|_| {
Response::builder()
.status(500)
.body("Failed to build response".as_bytes().to_vec())
.unwrap()
})
}
Err(e) => {
error!("Failed to get screenshot: {}", e);
Response::builder()
.status(500)
.body(format!("Error: {}", e).into_bytes())
.unwrap()
}
}
} else {
warn!("Invalid ambient-light URL format: {}", url_str);
Response::builder()
.status(400)
.body("Invalid URL format".as_bytes().to_vec())
.unwrap()
}
}
#[tokio::main]
async fn main() {
env_logger::init();
// Debug: Print available displays
match display_info::DisplayInfo::all() {
Ok(displays) => {
println!("=== AVAILABLE DISPLAYS ===");
for (index, display) in displays.iter().enumerate() {
println!(" Display {}: ID={}, Scale={}, Width={}, Height={}",
index, display.id, display.scale_factor, display.width, display.height);
}
println!("=== END DISPLAYS ===");
}
Err(e) => {
println!("Error getting display info: {}", e);
}
}
tokio::spawn(async move {
let screenshot_manager = ScreenshotManager::global().await;
screenshot_manager.start().await.unwrap_or_else(|e| {
@ -229,9 +364,17 @@ async fn main() {
led_color_publisher.start().await;
});
// Start WebSocket server for screen streaming
tokio::spawn(async move {
if let Err(e) = start_websocket_server().await {
error!("Failed to start WebSocket server: {}", e);
}
});
let _volume = VolumeManager::global().await;
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
greet,
list_display_info,
@ -248,143 +391,9 @@ async fn main() {
get_boards,
get_displays
])
.register_uri_scheme_protocol("ambient-light", move |_app, request| {
let response = ResponseBuilder::new().header("Access-Control-Allow-Origin", "*");
.register_uri_scheme_protocol("ambient-light", handle_ambient_light_protocol)
let uri = request.uri();
let uri = percent_encoding::percent_decode_str(uri)
.decode_utf8()
.unwrap()
.to_string();
let url = url_build_parse::parse_url(uri.as_str());
if let Err(err) = url {
error!("url parse error: {}", err);
return response
.status(500)
.mimetype("text/plain")
.body("Parse uri failed.".as_bytes().to_vec());
}
let url = url.unwrap();
let re = regex::Regex::new(r"^/displays/(\d+)$").unwrap();
let path = url.path;
let captures = re.captures(path.as_str());
if let None = captures {
error!("path not matched: {:?}", path);
return response
.status(404)
.mimetype("text/plain")
.body("Path Not Found.".as_bytes().to_vec());
}
let captures = captures.unwrap();
let display_id = captures[1].parse::<u32>().unwrap();
let bytes = tokio::task::block_in_place(move || {
tauri::async_runtime::block_on(async move {
let screenshot_manager = ScreenshotManager::global().await;
let rx: Result<tokio::sync::watch::Receiver<Screenshot>, anyhow::Error> =
screenshot_manager.subscribe_by_display_id(display_id).await;
if let Err(err) = rx {
anyhow::bail!("Display#{}: not found. {}", display_id, err);
}
let mut rx = rx.unwrap();
if rx.changed().await.is_err() {
anyhow::bail!("Display#{}: no more screenshot.", display_id);
}
let screenshot = rx.borrow().clone();
let bytes = screenshot.bytes.read().await;
if bytes.len() == 0 {
anyhow::bail!("Display#{}: no screenshot.", display_id);
}
log::debug!("Display#{}: screenshot size: {}", display_id, bytes.len());
let (scale_factor_x, scale_factor_y, width, height) = if url.query.is_some()
&& url.query.as_ref().unwrap().contains_key("height")
&& url.query.as_ref().unwrap().contains_key("width")
{
let width = url.query.as_ref().unwrap()["width"]
.parse::<u32>()
.map_err(|err| {
warn!("width parse error: {}", err);
err
})?;
let height = url.query.as_ref().unwrap()["height"]
.parse::<u32>()
.map_err(|err| {
warn!("height parse error: {}", err);
err
})?;
(
screenshot.width as f32 / width as f32,
screenshot.height as f32 / height as f32,
width,
height,
)
} else {
log::debug!("scale by scale_factor");
let scale_factor = screenshot.scale_factor;
(
scale_factor,
scale_factor,
(screenshot.width as f32 / scale_factor) as u32,
(screenshot.height as f32 / scale_factor) as u32,
)
};
log::debug!(
"scale by query. width: {}, height: {}, scale_factor: {}, len: {}",
width,
height,
screenshot.width as f32 / width as f32,
width * height * 4,
);
let bytes_per_row = screenshot.bytes_per_row as f32;
let mut rgba_buffer = vec![0u8; (width * height * 4) as usize];
for y in 0..height {
for x in 0..width {
let offset = ((y as f32) * scale_factor_y).floor() as usize
* bytes_per_row as usize
+ ((x as f32) * scale_factor_x).floor() as usize * 4;
let b = bytes[offset];
let g = bytes[offset + 1];
let r = bytes[offset + 2];
let a = bytes[offset + 3];
let offset_2 = (y * width + x) as usize * 4;
rgba_buffer[offset_2] = r;
rgba_buffer[offset_2 + 1] = g;
rgba_buffer[offset_2 + 2] = b;
rgba_buffer[offset_2 + 3] = a;
}
}
Ok(rgba_buffer.clone())
})
});
if let Ok(bytes) = bytes {
return response
.mimetype("octet/stream")
.status(200)
.body(bytes.to_vec());
}
let err = bytes.unwrap_err();
error!("request screenshot bin data failed: {}", err);
return response
.mimetype("text/plain")
.status(500)
.body(err.to_string().into_bytes());
})
.setup(move |app| {
let app_handle = app.handle().clone();
tokio::spawn(async move {
@ -400,7 +409,7 @@ async fn main() {
let config = config_update_receiver.borrow().clone();
app_handle.emit_all("config_changed", config).unwrap();
app_handle.emit("config_changed", config).unwrap();
}
});
@ -417,7 +426,7 @@ async fn main() {
let publisher = publisher_update_receiver.borrow().clone();
app_handle
.emit_all("led_sorted_colors_changed", publisher)
.emit("led_sorted_colors_changed", publisher)
.unwrap();
}
});
@ -435,7 +444,7 @@ async fn main() {
let publisher = publisher_update_receiver.borrow().clone();
app_handle
.emit_all("led_colors_changed", publisher)
.emit("led_colors_changed", publisher)
.unwrap();
}
});
@ -456,7 +465,7 @@ async fn main() {
let boards = boards.into_iter().collect::<Vec<_>>();
app_handle.emit_all("boards_changed", boards).unwrap();
app_handle.emit("boards_changed", boards).unwrap();
}
}
Err(err) => {
@ -477,7 +486,7 @@ async fn main() {
log::info!("displays changed. emit displays_changed event.");
app_handle.emit_all("displays_changed", displays).unwrap();
app_handle.emit("displays_changed", displays).unwrap();
}
});
@ -486,3 +495,30 @@ async fn main() {
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
// WebSocket server for screen streaming
async fn start_websocket_server() -> anyhow::Result<()> {
use tokio::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:8765").await?;
info!("WebSocket server listening on ws://127.0.0.1:8765");
while let Ok((stream, addr)) = listener.accept().await {
info!("New WebSocket connection from: {}", addr);
tokio::spawn(async move {
info!("Starting WebSocket handler for connection from: {}", addr);
match screen_stream::handle_websocket_connection(stream).await {
Ok(_) => {
info!("WebSocket connection from {} completed successfully", addr);
}
Err(e) => {
warn!("WebSocket connection error from {}: {}", addr, e);
}
}
info!("WebSocket handler task completed for: {}", addr);
});
}
Ok(())
}

View File

@ -0,0 +1,520 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::io::Cursor;
use anyhow::Result;
use image::{ImageFormat, RgbaImage};
use tokio::sync::{broadcast, RwLock};
use tokio::time::sleep;
use tokio_tungstenite::{accept_async, tungstenite::Message};
use futures_util::{SinkExt, StreamExt};
use crate::screenshot::Screenshot;
use crate::screenshot_manager::ScreenshotManager;
#[derive(Debug, Clone)]
pub struct StreamConfig {
pub display_id: u32,
pub target_width: u32,
pub target_height: u32,
pub quality: u8, // JPEG quality 1-100
pub max_fps: u8, // Maximum frames per second
}
impl Default for StreamConfig {
fn default() -> Self {
Self {
display_id: 0,
target_width: 320, // Reduced from 400 for better performance
target_height: 180, // Reduced from 225 for better performance
quality: 50, // Reduced from 75 for faster compression
max_fps: 15,
}
}
}
#[derive(Debug, Clone)]
pub struct StreamFrame {
pub display_id: u32,
pub timestamp: Instant,
pub jpeg_data: Vec<u8>,
pub width: u32,
pub height: u32,
}
pub struct ScreenStreamManager {
streams: Arc<RwLock<HashMap<u32, Arc<RwLock<StreamState>>>>>,
}
struct StreamState {
config: StreamConfig,
subscribers: Vec<broadcast::Sender<StreamFrame>>,
last_frame: Option<StreamFrame>,
last_screenshot_hash: Option<u64>,
last_force_send: Instant,
is_running: bool,
}
impl ScreenStreamManager {
pub fn new() -> Self {
Self {
streams: Arc::new(RwLock::new(HashMap::new())),
}
}
pub async fn start_stream(&self, config: StreamConfig) -> Result<broadcast::Receiver<StreamFrame>> {
let display_id = config.display_id;
let mut streams = self.streams.write().await;
if let Some(stream_state) = streams.get(&display_id) {
// Stream already exists, just add a new subscriber
let mut state = stream_state.write().await;
let (tx, rx) = broadcast::channel(10);
state.subscribers.push(tx);
return Ok(rx);
}
// Create new stream
let (tx, rx) = broadcast::channel(10);
let stream_state = Arc::new(RwLock::new(StreamState {
config: config.clone(),
subscribers: vec![tx],
last_frame: None,
last_screenshot_hash: None,
last_force_send: Instant::now(),
is_running: false,
}));
streams.insert(display_id, stream_state.clone());
drop(streams);
// Start the stream processing task
let streams_ref = self.streams.clone();
tokio::spawn(async move {
if let Err(e) = Self::run_stream(display_id, streams_ref).await {
log::error!("Stream {} error: {}", display_id, e);
}
});
Ok(rx)
}
async fn run_stream(display_id: u32, streams: Arc<RwLock<HashMap<u32, Arc<RwLock<StreamState>>>>>) -> Result<()> {
log::info!("Starting stream for display_id: {}", display_id);
let screenshot_manager = ScreenshotManager::global().await;
// If display_id is 0, try to get the first available display
let actual_display_id = if display_id == 0 {
// Get available displays and use the first one
let displays = display_info::DisplayInfo::all().map_err(|e| anyhow::anyhow!("Failed to get displays: {}", e))?;
if displays.is_empty() {
return Err(anyhow::anyhow!("No displays available"));
}
log::info!("Using first available display: {}", displays[0].id);
displays[0].id
} else {
display_id
};
log::info!("Attempting to subscribe to display_id: {}", actual_display_id);
let screenshot_rx = match screenshot_manager.subscribe_by_display_id(actual_display_id).await {
Ok(rx) => {
log::info!("Successfully subscribed to display_id: {}", actual_display_id);
rx
}
Err(e) => {
log::error!("Failed to subscribe to display_id {}: {}", actual_display_id, e);
return Err(e);
}
};
let mut screenshot_rx = screenshot_rx;
// Mark stream as running
{
let streams_lock = streams.read().await;
if let Some(stream_state) = streams_lock.get(&display_id) {
let mut state = stream_state.write().await;
state.is_running = true;
}
}
let mut last_process_time = Instant::now();
loop {
// Check if stream still has subscribers and is still running
let should_continue = {
let streams_lock = streams.read().await;
if let Some(stream_state) = streams_lock.get(&display_id) {
let state = stream_state.read().await;
!state.subscribers.is_empty() && state.is_running
} else {
false
}
};
if !should_continue {
break;
}
// Wait for new screenshot
if let Ok(_) = screenshot_rx.changed().await {
let screenshot = screenshot_rx.borrow().clone();
// Rate limiting based on max_fps
let config = {
let streams_lock = streams.read().await;
if let Some(stream_state) = streams_lock.get(&display_id) {
let state = stream_state.read().await;
state.config.clone()
} else {
break;
}
};
let min_interval = Duration::from_millis(1000 / config.max_fps as u64);
let elapsed = last_process_time.elapsed();
if elapsed < min_interval {
sleep(min_interval - elapsed).await;
}
// Process screenshot into JPEG frame
if let Ok(frame) = Self::process_screenshot(&screenshot, &config).await {
last_process_time = Instant::now();
// Check if frame content changed (simple hash comparison) or force send
let frame_hash = Self::calculate_frame_hash(&frame.jpeg_data);
let should_send = {
let streams_lock = streams.read().await;
if let Some(stream_state) = streams_lock.get(&display_id) {
let mut state = stream_state.write().await;
let changed = state.last_screenshot_hash.map_or(true, |hash| hash != frame_hash);
let elapsed_ms = state.last_force_send.elapsed().as_millis();
let force_send = elapsed_ms > 500; // Force send every 500ms for better CPU performance
if changed || force_send {
state.last_screenshot_hash = Some(frame_hash);
state.last_frame = Some(frame.clone());
if force_send {
state.last_force_send = Instant::now();
}
}
changed || force_send
} else {
false
}
};
if should_send {
// Send to all subscribers
let streams_lock = streams.read().await;
if let Some(stream_state) = streams_lock.get(&display_id) {
let state = stream_state.read().await;
for tx in state.subscribers.iter() {
if let Err(_) = tx.send(frame.clone()) {
log::warn!("Failed to send frame to subscriber for display_id: {}", display_id);
}
}
}
}
}
}
}
// Mark stream as stopped
{
let streams_lock = streams.read().await;
if let Some(stream_state) = streams_lock.get(&display_id) {
let mut state = stream_state.write().await;
state.is_running = false;
}
}
Ok(())
}
async fn process_screenshot(screenshot: &Screenshot, config: &StreamConfig) -> Result<StreamFrame> {
let total_start = Instant::now();
let bytes = screenshot.bytes.read().await;
// Convert BGRA to RGBA using unsafe with optimized batch processing for maximum performance
let mut rgba_bytes = bytes.as_ref().clone();
unsafe {
let ptr = rgba_bytes.as_mut_ptr() as *mut u32;
let len = rgba_bytes.len() / 4;
// Process in larger chunks of 64 for better cache efficiency and loop unrolling
let chunk_size = 64;
let full_chunks = len / chunk_size;
let remainder = len % chunk_size;
// Process full chunks with manual loop unrolling
for chunk_idx in 0..full_chunks {
let base_ptr = ptr.add(chunk_idx * chunk_size);
// Unroll the inner loop for better performance
for i in (0..chunk_size).step_by(4) {
// Process 4 pixels at once
let p0 = base_ptr.add(i).read();
let p1 = base_ptr.add(i + 1).read();
let p2 = base_ptr.add(i + 2).read();
let p3 = base_ptr.add(i + 3).read();
// BGRA (0xAABBGGRR) -> RGBA (0xAAGGBBRR)
let s0 = (p0 & 0xFF00FF00) | ((p0 & 0x00FF0000) >> 16) | ((p0 & 0x000000FF) << 16);
let s1 = (p1 & 0xFF00FF00) | ((p1 & 0x00FF0000) >> 16) | ((p1 & 0x000000FF) << 16);
let s2 = (p2 & 0xFF00FF00) | ((p2 & 0x00FF0000) >> 16) | ((p2 & 0x000000FF) << 16);
let s3 = (p3 & 0xFF00FF00) | ((p3 & 0x00FF0000) >> 16) | ((p3 & 0x000000FF) << 16);
base_ptr.add(i).write(s0);
base_ptr.add(i + 1).write(s1);
base_ptr.add(i + 2).write(s2);
base_ptr.add(i + 3).write(s3);
}
}
// Process remaining pixels
let remainder_start = full_chunks * chunk_size;
for i in 0..remainder {
let idx = remainder_start + i;
let pixel = ptr.add(idx).read();
let swapped = (pixel & 0xFF00FF00) | ((pixel & 0x00FF0000) >> 16) | ((pixel & 0x000000FF) << 16);
ptr.add(idx).write(swapped);
}
}
// Create image from raw bytes
let img = RgbaImage::from_raw(
screenshot.width,
screenshot.height,
rgba_bytes,
).ok_or_else(|| anyhow::anyhow!("Failed to create image from raw bytes"))?;
// Resize if needed
let final_img = if screenshot.width != config.target_width || screenshot.height != config.target_height {
image::imageops::resize(
&img,
config.target_width,
config.target_height,
image::imageops::FilterType::Nearest, // Fastest filter for real-time streaming
)
} else {
img
};
// Convert to JPEG
let mut jpeg_buffer = Vec::new();
let mut cursor = Cursor::new(&mut jpeg_buffer);
let rgb_img = image::DynamicImage::ImageRgba8(final_img).to_rgb8();
rgb_img.write_to(&mut cursor, ImageFormat::Jpeg)?;
let total_duration = total_start.elapsed();
log::debug!("Screenshot processed for display {} in {}ms, JPEG size: {} bytes",
config.display_id, total_duration.as_millis(), jpeg_buffer.len());
Ok(StreamFrame {
display_id: config.display_id,
timestamp: Instant::now(),
jpeg_data: jpeg_buffer,
width: config.target_width,
height: config.target_height,
})
}
fn calculate_frame_hash(data: &[u8]) -> u64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
// Sample every 100th byte for better sensitivity (was 1000)
for (i, &byte) in data.iter().enumerate() {
if i % 100 == 0 {
byte.hash(&mut hasher);
}
}
hasher.finish()
}
pub async fn stop_stream(&self, display_id: u32) {
log::info!("Stopping stream for display_id: {}", display_id);
let mut streams = self.streams.write().await;
if let Some(stream_state) = streams.get(&display_id) {
// Mark stream as not running to stop the processing task
let mut state = stream_state.write().await;
state.is_running = false;
log::info!("Marked stream as not running for display_id: {}", display_id);
}
// Remove the stream from the map
streams.remove(&display_id);
log::info!("Removed stream from manager for display_id: {}", display_id);
}
}
// Global instance
static SCREEN_STREAM_MANAGER: tokio::sync::OnceCell<ScreenStreamManager> = tokio::sync::OnceCell::const_new();
impl ScreenStreamManager {
pub async fn global() -> &'static Self {
SCREEN_STREAM_MANAGER.get_or_init(|| async {
ScreenStreamManager::new()
}).await
}
}
// WebSocket handler for screen streaming
pub async fn handle_websocket_connection(
stream: tokio::net::TcpStream,
) -> Result<()> {
log::info!("Accepting WebSocket connection...");
let ws_stream = match accept_async(stream).await {
Ok(ws) => {
log::info!("WebSocket handshake completed successfully");
ws
}
Err(e) => {
log::error!("WebSocket handshake failed: {}", e);
return Err(e.into());
}
};
let (ws_sender, mut ws_receiver) = ws_stream.split();
log::info!("WebSocket connection established, waiting for configuration...");
// Wait for the first configuration message
let config = loop {
// Add timeout to prevent hanging
let timeout_duration = tokio::time::Duration::from_secs(10);
match tokio::time::timeout(timeout_duration, ws_receiver.next()).await {
Ok(Some(msg)) => {
match msg {
Ok(Message::Text(text)) => {
log::info!("Received configuration message: {}", text);
if let Ok(config_json) = serde_json::from_str::<serde_json::Value>(&text) {
// Parse configuration from JSON
let display_id = config_json.get("display_id")
.and_then(|v| v.as_u64())
.unwrap_or(0) as u32;
let width = config_json.get("width")
.and_then(|v| v.as_u64())
.unwrap_or(320) as u32; // Reduced from 400 for better performance
let height = config_json.get("height")
.and_then(|v| v.as_u64())
.unwrap_or(180) as u32; // Reduced from 225 for better performance
let quality = config_json.get("quality")
.and_then(|v| v.as_u64())
.unwrap_or(50) as u8; // Reduced from 75 for faster compression
let config = StreamConfig {
display_id,
target_width: width,
target_height: height,
quality,
max_fps: 15,
};
log::info!("Parsed stream config: display_id={}, width={}, height={}, quality={}",
display_id, width, height, quality);
break config;
} else {
log::warn!("Failed to parse configuration JSON: {}", text);
}
}
Ok(Message::Close(_)) => {
log::info!("WebSocket connection closed before configuration");
return Ok(());
}
Err(e) => {
log::warn!("WebSocket error while waiting for config: {}", e);
return Err(e.into());
}
_ => {}
}
}
Ok(None) => {
log::warn!("WebSocket connection closed while waiting for configuration");
return Ok(());
}
Err(_) => {
log::warn!("Timeout waiting for WebSocket configuration message");
return Err(anyhow::anyhow!("Timeout waiting for configuration"));
}
}
};
// Start the stream with the received configuration
log::info!("Starting stream with config: display_id={}, width={}, height={}",
config.display_id, config.target_width, config.target_height);
let stream_manager = ScreenStreamManager::global().await;
let display_id_for_cleanup = config.display_id;
let mut frame_rx = match stream_manager.start_stream(config).await {
Ok(rx) => {
log::info!("Screen stream started successfully");
rx
}
Err(e) => {
log::error!("Failed to start screen stream: {}", e);
return Err(e);
}
};
// Handle incoming WebSocket messages (for control)
let ws_sender = Arc::new(tokio::sync::Mutex::new(ws_sender));
let ws_sender_clone = ws_sender.clone();
// Task to handle outgoing frames
let frame_task = tokio::spawn(async move {
while let Ok(frame) = frame_rx.recv().await {
let mut sender = ws_sender_clone.lock().await;
match sender.send(Message::Binary(frame.jpeg_data)).await {
Ok(_) => {},
Err(e) => {
log::warn!("Failed to send frame: {}", e);
break;
}
}
}
log::info!("Frame sending task completed");
});
// Task to handle incoming messages
let control_task = tokio::spawn(async move {
while let Some(msg) = ws_receiver.next().await {
match msg {
Ok(Message::Text(text)) => {
log::info!("Received control message: {}", text);
// Additional configuration updates could be handled here
}
Ok(Message::Close(_)) => {
log::info!("WebSocket connection closed");
break;
}
Err(e) => {
log::warn!("WebSocket error: {}", e);
break;
}
_ => {}
}
}
log::info!("Control message task completed");
});
// Wait for either task to complete
tokio::select! {
_ = frame_task => {},
_ = control_task => {},
}
// Clean up resources when connection ends
log::info!("WebSocket connection ending, cleaning up resources for display_id: {}", display_id_for_cleanup);
let stream_manager = ScreenStreamManager::global().await;
stream_manager.stop_stream(display_id_for_cleanup).await;
log::info!("WebSocket connection handler completed");
Ok(())
}

View File

@ -145,9 +145,16 @@ impl Screenshot {
for (x, y) in led_points {
// log::info!("x: {}, y: {}, bytes_per_row: {}", x, y, bytes_per_row);
let position = x * 4 + y * bytes_per_row;
b += bitmap[position] as f64;
g += bitmap[position + 1] as f64;
r += bitmap[position + 2] as f64;
// Add bounds checking to prevent index out of bounds
if position + 2 < bitmap.len() {
b += bitmap[position] as f64;
g += bitmap[position + 1] as f64;
r += bitmap[position + 2] as f64;
} else {
// Skip invalid positions or use default values
log::warn!("Invalid pixel position: x={}, y={}, position={}, bitmap_len={}", x, y, position, bitmap.len());
}
}
let color = LedColor::new((r / len) as u8, (g / len) as u8, (b / len) as u8);
colors.push(color);
@ -169,9 +176,16 @@ impl Screenshot {
for (x, y) in led_points {
// log::info!("x: {}, y: {}, bytes_per_row: {}", x, y, bytes_per_row);
let position = x * 4 + y * bytes_per_row;
b += bitmap[position] as f64;
g += bitmap[position + 1] as f64;
r += bitmap[position + 2] as f64;
// Add bounds checking to prevent index out of bounds
if position + 2 < bitmap.len() as usize {
b += bitmap[position] as f64;
g += bitmap[position + 1] as f64;
r += bitmap[position + 2] as f64;
} else {
// Skip invalid positions or use default values
log::warn!("Invalid pixel position in CG image: x={}, y={}, position={}, bitmap_len={}", x, y, position, bitmap.len());
}
// log::info!("position: {}, total: {}", position, bitmap.len());
}
let color = LedColor::new((r / len) as u8, (g / len) as u8, (b / len) as u8);

View File

@ -6,9 +6,11 @@ use core_graphics::display::{
};
use core_graphics::geometry::{CGPoint, CGRect, CGSize};
use paris::{info, warn};
use rust_swift_screencapture::display::CGDisplayId;
use screen_capture_kit::shareable_content::{SCDisplay, SCShareableContent};
use screen_capture_kit::stream::{SCStream, SCStreamConfiguration, SCContentFilter, SCStreamOutput};
use screen_capture_kit::stream::SCStreamDelegate;
use tauri::async_runtime::RwLock;
use tokio::sync::{broadcast, watch, Mutex, OnceCell};
use tokio::sync::{broadcast, watch, OnceCell};
use tokio::task::yield_now;
use tokio::time::sleep;
@ -20,7 +22,7 @@ pub fn get_display_colors(
sample_points: &Vec<Vec<LedSamplePoints>>,
bound_scale_factor: f32,
) -> anyhow::Result<Vec<LedColor>> {
log::debug!("take_screenshot");
let cg_display = CGDisplay::new(display_id);
let mut colors = vec![];
@ -106,20 +108,28 @@ impl ScreenshotManager {
pub async fn start(&self) -> anyhow::Result<()> {
let displays = display_info::DisplayInfo::all()?;
log::info!("ScreenshotManager starting with {} displays:", displays.len());
for display in &displays {
log::info!(" Display ID: {}, Scale: {}", display.id, display.scale_factor);
}
let futures = displays.iter().map(|display| async {
self.start_one(display.id, display.scale_factor)
.await
.unwrap_or_else(|err| {
warn!("start_one failed: display_id: {}, err: {}", display.id, err);
});
info!("start_one finished: display_id: {}", display.id);
});
futures::future::join_all(futures).await;
log::info!("ScreenshotManager started successfully");
Ok(())
}
async fn start_one(&self, display_id: u32, scale_factor: f32) -> anyhow::Result<()> {
log::info!("Starting screenshot capture for display_id: {}", display_id);
let merged_screenshot_tx = self.merged_screenshot_tx.clone();
let (tx, _) = watch::channel(Screenshot::new(
@ -138,45 +148,98 @@ impl ScreenshotManager {
drop(channels);
// Implement screen capture using screen-capture-kit
loop {
let display = rust_swift_screencapture::display::Display::new(display_id);
let mut frame_rx = display.subscribe_frame().await;
match Self::capture_display_screenshot(display_id, scale_factor).await {
Ok(screenshot) => {
let tx_for_send = tx.read().await;
let merged_screenshot_tx = merged_screenshot_tx.write().await;
display.start_capture(30).await;
let tx_for_send = tx.read().await;
while frame_rx.changed().await.is_ok() {
let frame = frame_rx.borrow().clone();
let screenshot = Screenshot::new(
display_id,
frame.height as u32,
frame.width as u32,
frame.bytes_per_row as usize,
frame.bytes,
scale_factor,
scale_factor,
);
let merged_screenshot_tx = merged_screenshot_tx.write().await;
if let Err(err) = merged_screenshot_tx.send(screenshot.clone()) {
// log::warn!("merged_screenshot_tx.send failed: {}", err);
}
if let Err(err) = tx_for_send.send(screenshot.clone()) {
log::warn!("display {} screenshot_tx.send failed: {}", display_id, err);
} else {
log::debug!("screenshot: {:?}", screenshot);
if let Err(err) = merged_screenshot_tx.send(screenshot.clone()) {
// log::warn!("merged_screenshot_tx.send failed: {}", err);
}
if let Err(err) = tx_for_send.send(screenshot.clone()) {
log::warn!("display {} screenshot_tx.send failed: {}", display_id, err);
}
}
Err(err) => {
warn!("Failed to capture screenshot for display {}: {}", display_id, err);
// Create a fallback empty screenshot to maintain the interface
let screenshot = Screenshot::new(
display_id,
1080,
1920,
1920 * 4, // Assuming RGBA format
Arc::new(vec![0u8; 1920 * 1080 * 4]),
scale_factor,
scale_factor,
);
yield_now().await;
let tx_for_send = tx.read().await;
let merged_screenshot_tx = merged_screenshot_tx.write().await;
if let Err(err) = merged_screenshot_tx.send(screenshot.clone()) {
// log::warn!("merged_screenshot_tx.send failed: {}", err);
}
if let Err(err) = tx_for_send.send(screenshot.clone()) {
log::warn!("display {} screenshot_tx.send failed: {}", display_id, err);
}
}
}
sleep(Duration::from_secs(5)).await;
info!(
"display {} frame_rx.changed() failed, try to restart",
display_id
);
// Sleep for a frame duration (5 FPS for much better CPU performance)
sleep(Duration::from_millis(200)).await;
yield_now().await;
}
}
async fn capture_display_screenshot(display_id: u32, scale_factor: f32) -> anyhow::Result<Screenshot> {
// For now, use the existing CGDisplay approach as a fallback
// TODO: Implement proper screen-capture-kit integration
let cg_display = CGDisplay::new(display_id);
let bounds = cg_display.bounds();
let cg_image = CGDisplay::screenshot(
bounds,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID,
kCGWindowImageDefault,
)
.ok_or_else(|| anyhow::anyhow!("Display#{}: take screenshot failed - possibly no screen recording permission", display_id))?;
let bitmap = cg_image.data();
let width = cg_image.width() as u32;
let height = cg_image.height() as u32;
let bytes_per_row = cg_image.bytes_per_row();
// Convert CFData to Vec<u8>
let data_ptr = bitmap.bytes().as_ptr();
let data_len = bitmap.len() as usize;
let screenshot_data = unsafe {
std::slice::from_raw_parts(data_ptr, data_len).to_vec()
};
Ok(Screenshot::new(
display_id,
height,
width,
bytes_per_row,
Arc::new(screenshot_data),
scale_factor,
scale_factor,
))
}
pub fn get_sorted_colors(colors: &Vec<u8>, mappers: &Vec<SamplePointMapper>) -> Vec<u8> {
let total_leds = mappers
.iter()
@ -232,7 +295,7 @@ impl ScreenshotManager {
pub async fn subscribe_by_display_id(
&self,
display_id: CGDisplayId,
display_id: u32,
) -> anyhow::Result<watch::Receiver<Screenshot>> {
let channels = self.channels.read().await;
if let Some(tx) = channels.get(&display_id) {

View File

@ -1,43 +1,23 @@
{
"$schema": "https://schema.tauri.app/config/2.0.0",
"productName": "test-demo",
"version": "0.0.1",
"identifier": "cc.ivanli.ambient-light.desktop",
"build": {
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build",
"devPath": "http://localhost:1420",
"distDir": "../dist",
"withGlobalTauri": false
"devUrl": "http://localhost:1420",
"frontendDist": "../dist"
},
"package": {
"productName": "test-demo",
"version": "0.0.1"
},
"tauri": {
"allowlist": {
"all": false,
"shell": {
"all": false,
"open": true
}
},
"bundle": {
"active": true,
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "cc.ivanli.ambient-light.desktop",
"targets": "all",
"macOS": {
"minimumSystemVersion": "13"
}
},
"app": {
"withGlobalTauri": true,
"security": {
"csp": null
},
"updater": {
"active": false
"csp": null,
"assetProtocol": {
"scope": [
"**"
]
}
},
"windows": [
{
@ -48,5 +28,19 @@
"height": 600
}
]
},
"bundle": {
"active": true,
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"targets": "all",
"macOS": {
"minimumSystemVersion": "13"
}
}
}

View File

@ -2,7 +2,7 @@ import { Routes, Route } from '@solidjs/router';
import { LedStripConfiguration } from './components/led-strip-configuration/led-strip-configuration';
import { WhiteBalance } from './components/white-balance/white-balance';
import { createEffect } from 'solid-js';
import { invoke } from '@tauri-apps/api';
import { invoke } from '@tauri-apps/api/core';
import { setLedStripStore } from './stores/led-strip.store';
import { LedStripConfigContainer } from './models/led-strip-config';
import { InfoIndex } from './components/info/info-index';
@ -11,29 +11,59 @@ import { DisplayStateIndex } from './components/displays/display-state-index';
function App() {
createEffect(() => {
invoke<LedStripConfigContainer>('read_config').then((config) => {
console.log('read config', config);
console.log('App: read config', config);
setLedStripStore({
strips: config.strips,
mappers: config.mappers,
colorCalibration: config.color_calibration,
});
}).catch((error) => {
console.error('App: Failed to read config:', error);
});
});
return (
<div>
<div>
<a href="/info"></a>
<a href="/displays"></a>
<a href="/led-strips-configuration"></a>
<a href="/white-balance"></a>
<div class="min-h-screen bg-base-100" data-theme="dark">
{/* Navigation */}
<div class="navbar bg-base-200 shadow-lg">
<div class="navbar-start">
<div class="dropdown">
<div tabindex="0" role="button" class="btn btn-ghost lg:hidden">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h8m-8 6h16"></path>
</svg>
</div>
<ul tabindex="0" class="menu menu-sm dropdown-content mt-3 z-[1] p-2 shadow bg-base-100 rounded-box w-52">
<li><a href="/info" class="text-base-content"></a></li>
<li><a href="/displays" class="text-base-content"></a></li>
<li><a href="/led-strips-configuration" class="text-base-content"></a></li>
<li><a href="/white-balance" class="text-base-content"></a></li>
</ul>
</div>
<a class="btn btn-ghost text-xl text-primary font-bold"></a>
</div>
<div class="navbar-center hidden lg:flex">
<ul class="menu menu-horizontal px-1">
<li><a href="/info" class="btn btn-ghost text-base-content hover:text-primary"></a></li>
<li><a href="/displays" class="btn btn-ghost text-base-content hover:text-primary"></a></li>
<li><a href="/led-strips-configuration" class="btn btn-ghost text-base-content hover:text-primary"></a></li>
<li><a href="/white-balance" class="btn btn-ghost text-base-content hover:text-primary"></a></li>
</ul>
</div>
<div class="navbar-end">
<div class="badge badge-primary badge-outline">v1.0</div>
</div>
</div>
<Routes>
<Route path="/info" component={InfoIndex} />
<Route path="/displays" component={DisplayStateIndex} />
<Route path="/led-strips-configuration" component={LedStripConfiguration} />
<Route path="/white-balance" component={WhiteBalance} />
</Routes>
{/* Main Content */}
<main class="container mx-auto p-4">
<Routes>
<Route path="/info" component={InfoIndex} />
<Route path="/displays" component={DisplayStateIndex} />
<Route path="/led-strips-configuration" component={LedStripConfiguration} />
<Route path="/white-balance" component={WhiteBalance} />
</Routes>
</main>
</div>
);
}

View File

@ -11,26 +11,59 @@ type ItemProps = {
const Item: ParentComponent<ItemProps> = (props) => {
return (
<dl class="flex">
<dt class="w-20">{props.label}</dt>
<dd class="flex-auto">{props.children}</dd>
</dl>
<div class="flex justify-between items-center py-1">
<dt class="text-sm font-medium text-base-content/70">{props.label}</dt>
<dd class="text-sm font-mono text-base-content">{props.children}</dd>
</div>
);
};
export const DisplayStateCard: Component<DisplayStateCardProps> = (props) => {
return (
<section class="p-2 rounded shadow">
<Item label="Brightness">{props.state.brightness}</Item>
<Item label="Max Brightness">{props.state.max_brightness}</Item>
<Item label="Min Brightness">{props.state.min_brightness}</Item>
<Item label="Contrast">{props.state.contrast}</Item>
<Item label="Max Contrast">{props.state.max_contrast}</Item>
<Item label="Min Contrast">{props.state.min_contrast}</Item>
<Item label="Max Mode">{props.state.max_mode}</Item>
<Item label="Min Mode">{props.state.min_mode}</Item>
<Item label="Mode">{props.state.mode}</Item>
<Item label="Last Modified At">{props.state.last_modified_at.toISOString()}</Item>
</section>
<div class="card bg-base-200 shadow-lg hover:shadow-xl transition-shadow duration-200">
<div class="card-body p-4">
<div class="card-title text-base mb-3 flex items-center justify-between">
<span></span>
<div class="badge badge-primary badge-outline"></div>
</div>
<div class="grid grid-cols-1 gap-3">
{/* 亮度信息 */}
<div class="bg-base-100 rounded-lg p-3">
<h4 class="text-sm font-semibold text-base-content mb-2"></h4>
<div class="space-y-1">
<Item label="当前亮度">{props.state.brightness}</Item>
<Item label="最大亮度">{props.state.max_brightness}</Item>
<Item label="最小亮度">{props.state.min_brightness}</Item>
</div>
</div>
{/* 对比度信息 */}
<div class="bg-base-100 rounded-lg p-3">
<h4 class="text-sm font-semibold text-base-content mb-2"></h4>
<div class="space-y-1">
<Item label="当前对比度">{props.state.contrast}</Item>
<Item label="最大对比度">{props.state.max_contrast}</Item>
<Item label="最小对比度">{props.state.min_contrast}</Item>
</div>
</div>
{/* 模式信息 */}
<div class="bg-base-100 rounded-lg p-3">
<h4 class="text-sm font-semibold text-base-content mb-2"></h4>
<div class="space-y-1">
<Item label="当前模式">{props.state.mode}</Item>
<Item label="最大模式">{props.state.max_mode}</Item>
<Item label="最小模式">{props.state.min_mode}</Item>
</div>
</div>
{/* 更新时间 */}
<div class="text-xs text-base-content/50 text-center pt-2 border-t border-base-300">
: {props.state.last_modified_at.toLocaleString()}
</div>
</div>
</div>
</div>
);
};

View File

@ -1,7 +1,7 @@
import { Component, For, createEffect, createSignal } from 'solid-js';
import { listen } from '@tauri-apps/api/event';
import debug from 'debug';
import { invoke } from '@tauri-apps/api';
import { invoke } from '@tauri-apps/api/core';
import { DisplayState, RawDisplayState } from '../../models/display-state.model';
import { DisplayStateCard } from './display-state-card';
@ -36,17 +36,37 @@ export const DisplayStateIndex: Component = () => {
};
});
return (
<ol class="grid sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 p-2 gap-2">
<For each={states()}>
{(state, index) => (
<li class="bg-slate-50 text-gray-800 relative border-2 border-slate-50 hover:border-sky-300 focus:border-sky-300 transition">
<DisplayStateCard state={state} />
<span class="absolute left-2 -top-3 bg-sky-300 text-white px-1 py-0.5 text-xs rounded-sm font-mono">
#{index() + 1}
</span>
</li>
)}
</For>
</ol>
<div class="space-y-6">
<div class="flex items-center justify-between">
<h1 class="text-2xl font-bold text-base-content"></h1>
<div class="stats shadow">
<div class="stat">
<div class="stat-title"></div>
<div class="stat-value text-primary">{states().length}</div>
</div>
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-6">
<For each={states()}>
{(state, index) => (
<div class="relative">
<DisplayStateCard state={state} />
<div class="absolute -top-2 -left-2 w-6 h-6 bg-primary text-primary-content rounded-full flex items-center justify-center text-xs font-bold">
{index() + 1}
</div>
</div>
)}
</For>
</div>
{states().length === 0 && (
<div class="text-center py-12">
<div class="text-6xl mb-4">🖥</div>
<h3 class="text-lg font-semibold text-base-content mb-2"></h3>
<p class="text-base-content/70"></p>
</div>
)}
</div>
);
};

View File

@ -2,7 +2,7 @@ import { Component, For, createEffect, createSignal } from 'solid-js';
import { BoardInfo } from '../../models/board-info.model';
import { listen } from '@tauri-apps/api/event';
import debug from 'debug';
import { invoke } from '@tauri-apps/api';
import { invoke } from '@tauri-apps/api/core';
import { BoardInfoPanel } from './board-info-panel';
const logger = debug('app:components:info:board-index');
@ -26,17 +26,37 @@ export const BoardIndex: Component = () => {
};
});
return (
<ol class="grid sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 p-2 gap-2">
<For each={boards()}>
{(board, index) => (
<li class="bg-slate-50 text-gray-800 relative border-2 border-slate-50 hover:border-sky-300 focus:border-sky-300 transition">
<BoardInfoPanel board={board} />
<span class="absolute left-2 -top-3 bg-sky-300 text-white px-1 py-0.5 text-xs rounded-sm font-mono">
#{index() + 1}
</span>
</li>
)}
</For>
</ol>
<div class="space-y-6">
<div class="flex items-center justify-between">
<h1 class="text-2xl font-bold text-base-content"></h1>
<div class="stats shadow">
<div class="stat">
<div class="stat-title"></div>
<div class="stat-value text-primary">{boards().length}</div>
</div>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<For each={boards()}>
{(board, index) => (
<div class="relative">
<BoardInfoPanel board={board} />
<div class="absolute -top-2 -left-2 w-6 h-6 bg-primary text-primary-content rounded-full flex items-center justify-center text-xs font-bold">
{index() + 1}
</div>
</div>
)}
</For>
</div>
{boards().length === 0 && (
<div class="text-center py-12">
<div class="text-6xl mb-4">🔍</div>
<h3 class="text-lg font-semibold text-base-content mb-2"></h3>
<p class="text-base-content/70"></p>
</div>
)}
</div>
);
};

View File

@ -7,10 +7,10 @@ type ItemProps = {
const Item: ParentComponent<ItemProps> = (props) => {
return (
<dl class="flex">
<dt class="w-20">{props.label}</dt>
<dd class="flex-auto">{props.children}</dd>
</dl>
<div class="flex justify-between items-center py-1">
<dt class="text-sm font-medium text-base-content/70">{props.label}</dt>
<dd class="text-sm font-mono text-base-content">{props.children}</dd>
</div>
);
};
@ -41,20 +41,31 @@ export const BoardInfoPanel: Component<{ board: BoardInfo }> = (props) => {
}
});
const statusBadgeClass = createMemo(() => {
const status = connectStatus();
if (status === 'Connected') {
return 'badge badge-success badge-sm';
} else if (status?.startsWith('Connecting')) {
return 'badge badge-warning badge-sm';
} else {
return 'badge badge-error badge-sm';
}
});
return (
<section class="p-2 rounded shadow">
<Item label="Host">{props.board.fullname}</Item>
<Item label="Host">{props.board.host}</Item>
<Item label="Ip Addr">
<span class="font-mono">{props.board.address}</span>
</Item>
<Item label="Port">
<span class="font-mono">{props.board.port}</span>
</Item>
<Item label="Status">
<span class="font-mono">{connectStatus()}</span>
</Item>
<Item label="TTL">{ttl()}</Item>
</section>
<div class="card bg-base-200 shadow-lg hover:shadow-xl transition-shadow duration-200">
<div class="card-body p-4">
<div class="card-title text-base mb-3 flex items-center justify-between">
<span class="truncate">{props.board.fullname}</span>
<div class={statusBadgeClass()}>{connectStatus()}</div>
</div>
<div class="space-y-2">
<Item label="主机名">{props.board.host}</Item>
<Item label="IP地址">{props.board.address}</Item>
<Item label="端口">{props.board.port}</Item>
<Item label="延迟">{ttl()}</Item>
</div>
</div>
</div>
);
};

View File

@ -7,10 +7,10 @@ type DisplayInfoItemProps = {
export const DisplayInfoItem: ParentComponent<DisplayInfoItemProps> = (props) => {
return (
<dl class="px-3 py-1 flex hover:bg-slate-900/50 gap-2 text-white drop-shadow-[0_2px_2px_rgba(0,0,0,0.8)] rounded">
<dt class="uppercase w-1/2 select-all whitespace-nowrap">{props.label}</dt>
<dd class="select-all w-1/2 whitespace-nowrap">{props.children}</dd>
</dl>
<div class="flex justify-between items-center py-1 px-2 hover:bg-base-300/50 rounded transition-colors">
<dt class="text-sm font-medium text-base-content/80 uppercase">{props.label}</dt>
<dd class="text-sm font-mono text-base-content select-all">{props.children}</dd>
</div>
);
};
@ -21,22 +21,29 @@ type DisplayInfoPanelProps = {
export const DisplayInfoPanel: Component<DisplayInfoPanelProps> = (props) => {
const [localProps, rootProps] = splitProps(props, ['display']);
return (
<section {...rootProps} class={'m-2 flex flex-col gap-1 py-2 ' + rootProps.class}>
<DisplayInfoItem label="ID">
<code>{localProps.display.id}</code>
</DisplayInfoItem>
<DisplayInfoItem label="Position">
({localProps.display.x}, {localProps.display.y})
</DisplayInfoItem>
<DisplayInfoItem label="Size">
{localProps.display.width} x {localProps.display.height}
</DisplayInfoItem>
<DisplayInfoItem label="Scale Factor">
{localProps.display.scale_factor}
</DisplayInfoItem>
<DisplayInfoItem label="is Primary">
{localProps.display.is_primary ? 'True' : 'False'}
</DisplayInfoItem>
</section>
<div {...rootProps} class={'card bg-base-100/95 backdrop-blur shadow-lg border border-base-300 ' + rootProps.class}>
<div class="card-body p-4">
<div class="card-title text-sm mb-3 flex items-center justify-between">
<span class="text-base-content"></span>
{localProps.display.is_primary && (
<div class="badge badge-primary badge-sm"></div>
)}
</div>
<div class="space-y-1">
<DisplayInfoItem label="ID">
<code class="bg-base-200 px-1 rounded text-xs">{localProps.display.id}</code>
</DisplayInfoItem>
<DisplayInfoItem label="位置">
({localProps.display.x}, {localProps.display.y})
</DisplayInfoItem>
<DisplayInfoItem label="尺寸">
{localProps.display.width} × {localProps.display.height}
</DisplayInfoItem>
<DisplayInfoItem label="缩放">
{localProps.display.scale_factor}×
</DisplayInfoItem>
</div>
</div>
</div>
);
};

View File

@ -6,6 +6,7 @@ import { DisplayInfoPanel } from './display-info-panel';
import { LedStripPart } from './led-strip-part';
import { ScreenView } from './screen-view';
type DisplayViewProps = {
display: DisplayInfo;
};
@ -23,7 +24,6 @@ export const DisplayView: Component<DisplayViewProps> = (props) => {
}));
const ledStripConfigs = createMemo(() => {
console.log('ledStripConfigs', ledStripStore.strips);
return ledStripStore.strips.filter((c) => c.display_id === props.display.id);
});

View File

@ -0,0 +1,155 @@
import { invoke } from '@tauri-apps/api/core';
import { Component, createMemo, For, JSX, splitProps } from 'solid-js';
import { DisplayInfo } from '../../models/display-info.model';
import { ledStripStore } from '../../stores/led-strip.store';
import { Borders } from '../../constants/border';
type LedCountControlItemProps = {
displayId: number;
border: Borders;
label: string;
};
const LedCountControlItem: Component<LedCountControlItemProps> = (props) => {
const config = createMemo(() => {
return ledStripStore.strips.find(
(s) => s.display_id === props.displayId && s.border === props.border
);
});
const handleDecrease = () => {
if (config()) {
invoke('patch_led_strip_len', {
displayId: props.displayId,
border: props.border,
deltaLen: -1,
}).catch((e) => {
console.error(e);
});
}
};
const handleIncrease = () => {
if (config()) {
invoke('patch_led_strip_len', {
displayId: props.displayId,
border: props.border,
deltaLen: 1,
}).catch((e) => {
console.error(e);
});
}
};
const handleInputChange = (e: Event) => {
const target = e.target as HTMLInputElement;
const newValue = parseInt(target.value);
const currentLen = config()?.len || 0;
if (!isNaN(newValue) && newValue >= 0 && newValue <= 1000) {
const deltaLen = newValue - currentLen;
if (deltaLen !== 0) {
invoke('patch_led_strip_len', {
displayId: props.displayId,
border: props.border,
deltaLen: deltaLen,
}).catch((e) => {
console.error(e);
// Reset input value on error
target.value = currentLen.toString();
});
}
} else {
// Reset invalid input
target.value = (config()?.len || 0).toString();
}
};
return (
<div class="card bg-base-100 border border-base-300/50 p-2">
<div class="flex flex-col gap-1">
<div class="text-center">
<span class="text-xs font-medium text-base-content">
{props.label}
</span>
</div>
<div class="flex items-center gap-1">
<button
class="btn btn-xs btn-circle btn-outline flex-shrink-0"
onClick={handleDecrease}
disabled={!config() || (config()?.len || 0) <= 0}
title="减少LED数量"
>
-
</button>
<input
type="number"
class="input input-xs flex-1 text-center min-w-0 text-sm font-medium [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
value={config()?.len || 0}
min="0"
max="1000"
onBlur={handleInputChange}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleInputChange(e);
}
}}
/>
<button
class="btn btn-xs btn-circle btn-outline flex-shrink-0"
onClick={handleIncrease}
disabled={!config() || (config()?.len || 0) >= 1000}
title="增加LED数量"
>
+
</button>
</div>
</div>
</div>
);
};
type LedCountControlPanelProps = {
display: DisplayInfo;
} & JSX.HTMLAttributes<HTMLElement>;
export const LedCountControlPanel: Component<LedCountControlPanelProps> = (props) => {
const [localProps, rootProps] = splitProps(props, ['display']);
const borders: { border: Borders; label: string }[] = [
{ border: 'Top', label: '上' },
{ border: 'Bottom', label: '下' },
{ border: 'Left', label: '左' },
{ border: 'Right', label: '右' },
];
return (
<div {...rootProps} class={'card bg-base-200 shadow-lg border border-base-300 ' + (rootProps.class || '')}>
<div class="card-body p-4">
<div class="card-title text-base mb-3 flex items-center justify-between">
<span>LED数量控制</span>
<div class="badge badge-info badge-outline"> {localProps.display.id}</div>
</div>
<div class="grid grid-cols-4 gap-2">
<For each={borders}>
{(item) => (
<LedCountControlItem
displayId={localProps.display.id}
border={item.border}
label={item.label}
/>
)}
</For>
</div>
<div class="text-xs text-base-content/50 mt-3 p-2 bg-base-300/50 rounded">
💡 +/- LED0-1000
</div>
</div>
</div>
);
};

View File

@ -1,5 +1,5 @@
import { createEffect, onCleanup } from 'solid-js';
import { invoke } from '@tauri-apps/api/tauri';
import { invoke } from '@tauri-apps/api/core';
import { DisplayView } from './display-view';
import { DisplayListContainer } from './display-list-container';
import { displayStore, setDisplayStore } from '../../stores/display.store';
@ -7,22 +7,31 @@ import { LedStripConfigContainer } from '../../models/led-strip-config';
import { setLedStripStore } from '../../stores/led-strip.store';
import { listen } from '@tauri-apps/api/event';
import { LedStripPartsSorter } from './led-strip-parts-sorter';
import { LedCountControlPanel } from './led-count-control-panel';
import { createStore } from 'solid-js/store';
import {
LedStripConfigurationContext,
LedStripConfigurationContextType,
} from '../../contexts/led-strip-configuration.context';
export const LedStripConfiguration = () => {
createEffect(() => {
invoke<string>('list_display_info').then((displays) => {
const parsedDisplays = JSON.parse(displays);
console.log('LedStripConfiguration: Loaded displays:', parsedDisplays);
setDisplayStore({
displays: JSON.parse(displays),
displays: parsedDisplays,
});
}).catch((error) => {
console.error('LedStripConfiguration: Failed to load displays:', error);
});
invoke<LedStripConfigContainer>('read_led_strip_configs').then((configs) => {
console.log(configs);
console.log('LedStripConfiguration: Loaded LED strip configs:', configs);
setLedStripStore(configs);
}).catch((error) => {
console.error('LedStripConfiguration: Failed to load LED strip configs:', error);
});
});
@ -30,7 +39,6 @@ export const LedStripConfiguration = () => {
createEffect(() => {
const unlisten = listen('config_changed', (event) => {
const { strips, mappers } = event.payload as LedStripConfigContainer;
console.log(event.payload);
setLedStripStore({
strips,
mappers,
@ -92,14 +100,65 @@ export const LedStripConfiguration = () => {
];
return (
<div>
<div class="space-y-6">
<div class="flex items-center justify-between">
<h1 class="text-2xl font-bold text-base-content"></h1>
<div class="stats shadow">
<div class="stat">
<div class="stat-title"></div>
<div class="stat-value text-primary">{displayStore.displays.length}</div>
</div>
</div>
</div>
<LedStripConfigurationContext.Provider value={ledStripConfigurationContextValue}>
<LedStripPartsSorter />
<DisplayListContainer>
{displayStore.displays.map((display) => {
return <DisplayView display={display} />;
})}
</DisplayListContainer>
{/* LED Strip Sorter Panel */}
<div class="card bg-base-200 shadow-lg">
<div class="card-body p-4">
<div class="card-title text-base mb-3">
<span></span>
<div class="badge badge-info badge-outline"></div>
</div>
<LedStripPartsSorter />
<div class="text-xs text-base-content/50 mt-2">
💡
</div>
</div>
</div>
{/* Display Configuration Panel */}
<div class="card bg-base-200 shadow-lg">
<div class="card-body p-4">
<div class="card-title text-base mb-3">
<span></span>
<div class="badge badge-secondary badge-outline"></div>
</div>
<div class="h-96 mb-4">
<DisplayListContainer>
{displayStore.displays.map((display) => {
console.log('LedStripConfiguration: Rendering DisplayView for display:', display);
return <DisplayView display={display} />;
})}
</DisplayListContainer>
</div>
<div class="text-xs text-base-content/50">
💡 使LED数量
</div>
</div>
</div>
{/* LED Count Control Panels */}
<div class="space-y-4">
<div class="flex items-center gap-2 mb-3">
<h2 class="text-lg font-semibold text-base-content">LED数量控制</h2>
<div class="badge badge-info badge-outline"></div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
{displayStore.displays.map((display) => (
<LedCountControlPanel display={display} />
))}
</div>
</div>
</LedStripConfigurationContext.Provider>
</div>
);

View File

@ -1,4 +1,4 @@
import { invoke } from '@tauri-apps/api';
import { invoke } from '@tauri-apps/api/core';
import {
Component,
createEffect,
@ -34,7 +34,7 @@ export const Pixel: Component<PixelProps> = (props) => {
title={props.color}
>
<div
class="absolute top-1/2 -translate-y-1/2 h-2.5 w-2.5 rounded-full ring-1 ring-stone-300"
class="absolute top-1/2 -translate-y-1/2 h-2.5 w-2.5 rounded-full ring-1 ring-stone-300/50"
style={style()}
/>
</div>
@ -60,21 +60,44 @@ export const LedStripPart: Component<LedStripPartProps> = (props) => {
);
if (index === -1) {
console.log('🔍 LED: Strip config not found', {
displayId: localProps.config.display_id,
border: localProps.config.border,
availableStrips: ledStripStore.strips.length
});
return;
}
const mapper = ledStripStore.mappers[index];
if (!mapper) {
console.log('🔍 LED: Mapper not found', { index, mappersCount: ledStripStore.mappers.length });
return;
}
const offset = mapper.pos * 3;
const offset = mapper.start * 3;
console.log('🎨 LED: Updating colors', {
displayId: localProps.config.display_id,
border: localProps.config.border,
stripLength: localProps.config.len,
mapperPos: mapper.pos,
offset,
colorsArrayLength: ledStripStore.colors.length,
firstFewColors: Array.from(ledStripStore.colors.slice(offset, offset + 9))
});
const colors = new Array(localProps.config.len).fill(null).map((_, i) => {
const index = offset + i * 3;
return `rgb(${ledStripStore.colors[index]}, ${ledStripStore.colors[index + 1]}, ${
ledStripStore.colors[index + 2]
})`;
const r = ledStripStore.colors[index] || 0;
const g = ledStripStore.colors[index + 1] || 0;
const b = ledStripStore.colors[index + 2] || 0;
return `rgb(${r}, ${g}, ${b})`;
});
console.log('🎨 LED: Generated colors', {
border: localProps.config.border,
colorsCount: colors.length,
sampleColors: colors.slice(0, 3)
});
setColors(colors);
@ -101,26 +124,14 @@ export const LedStripPart: Component<LedStripPartProps> = (props) => {
},
});
const onWheel = (e: WheelEvent) => {
if (localProps.config) {
invoke('patch_led_strip_len', {
displayId: localProps.config.display_id,
border: localProps.config.border,
deltaLen: e.deltaY > 0 ? 1 : -1,
})
.then(() => {})
.catch((e) => {
console.error(e);
});
}
};
return (
<section
{...rootProps}
ref={setAnchor}
class={
'flex rounded-full flex-nowrap justify-around items-center overflow-hidden ' +
'flex rounded-full flex-nowrap justify-around items-center overflow-hidden bg-gray-800/20 border border-gray-600/30 min-h-[32px] min-w-[32px] ' +
rootProps.class
}
classList={{
@ -129,7 +140,7 @@ export const LedStripPart: Component<LedStripPartProps> = (props) => {
stripConfiguration.selectedStripPart?.displayId ===
localProps.config?.display_id,
}}
onWheel={onWheel}
>
<For each={colors()}>{(item) => <Pixel color={item} />}</For>
</section>

View File

@ -16,7 +16,7 @@ import {
} from 'solid-js';
import { LedStripConfig, LedStripPixelMapper } from '../../models/led-strip-config';
import { ledStripStore } from '../../stores/led-strip.store';
import { invoke } from '@tauri-apps/api';
import { invoke } from '@tauri-apps/api/core';
import { LedStripConfigurationContext } from '../../contexts/led-strip-configuration.context';
import background from '../../assets/transparent-grid-background.svg?url';

View File

@ -0,0 +1,290 @@
import {
Component,
createEffect,
createSignal,
JSX,
onCleanup,
onMount,
splitProps,
} from 'solid-js';
import { invoke } from '@tauri-apps/api/core';
type ScreenViewWebSocketProps = {
displayId: number;
width?: number;
height?: number;
quality?: number;
} & JSX.HTMLAttributes<HTMLDivElement>;
export const ScreenViewWebSocket: Component<ScreenViewWebSocketProps> = (props) => {
const [localProps, rootProps] = splitProps(props, ['displayId', 'width', 'height', 'quality']);
let canvas: HTMLCanvasElement;
let root: HTMLDivElement;
const [ctx, setCtx] = createSignal<CanvasRenderingContext2D | null>(null);
const [drawInfo, setDrawInfo] = createSignal({
drawX: 0,
drawY: 0,
drawWidth: 0,
drawHeight: 0,
});
const [connectionStatus, setConnectionStatus] = createSignal<'connecting' | 'connected' | 'disconnected' | 'error'>('disconnected');
const [frameCount, setFrameCount] = createSignal(0);
const [lastFrameTime, setLastFrameTime] = createSignal(0);
const [fps, setFps] = createSignal(0);
let websocket: WebSocket | null = null;
let reconnectTimeout: number | null = null;
let isMounted = true;
// Performance monitoring
let frameTimestamps: number[] = [];
const connectWebSocket = () => {
if (!isMounted) {
console.log('Component not mounted, skipping WebSocket connection');
return;
}
const wsUrl = `ws://127.0.0.1:8765`;
console.log('Connecting to WebSocket:', wsUrl, 'with displayId:', localProps.displayId);
setConnectionStatus('connecting');
websocket = new WebSocket(wsUrl);
websocket.binaryType = 'arraybuffer';
console.log('WebSocket object created:', websocket);
websocket.onopen = () => {
console.log('WebSocket connected successfully!');
setConnectionStatus('connected');
// Send initial configuration
const config = {
display_id: localProps.displayId,
width: localProps.width || 320, // Reduced from 400 for better performance
height: localProps.height || 180, // Reduced from 225 for better performance
quality: localProps.quality || 50 // Reduced from 75 for faster compression
};
console.log('Sending WebSocket configuration:', config);
websocket?.send(JSON.stringify(config));
};
websocket.onmessage = (event) => {
console.log('🔍 WebSocket message received:', {
type: typeof event.data,
isArrayBuffer: event.data instanceof ArrayBuffer,
size: event.data instanceof ArrayBuffer ? event.data.byteLength : 'N/A'
});
if (event.data instanceof ArrayBuffer) {
console.log('📦 Processing ArrayBuffer frame, size:', event.data.byteLength);
handleJpegFrame(new Uint8Array(event.data));
} else {
console.log('⚠️ Received non-ArrayBuffer data:', event.data);
}
};
websocket.onclose = (event) => {
console.log('WebSocket closed:', event.code, event.reason);
setConnectionStatus('disconnected');
websocket = null;
// Auto-reconnect after 2 seconds if component is still mounted
if (isMounted && !reconnectTimeout) {
reconnectTimeout = window.setTimeout(() => {
reconnectTimeout = null;
connectWebSocket();
}, 2000);
}
};
websocket.onerror = (error) => {
console.error('WebSocket error:', error);
setConnectionStatus('error');
};
};
const handleJpegFrame = async (jpegData: Uint8Array) => {
const _ctx = ctx();
if (!_ctx) return;
try {
// Update performance metrics
const now = performance.now();
frameTimestamps.push(now);
// Keep only last 30 frames for FPS calculation
if (frameTimestamps.length > 30) {
frameTimestamps = frameTimestamps.slice(-30);
}
// Calculate FPS
if (frameTimestamps.length >= 2) {
const timeSpan = frameTimestamps[frameTimestamps.length - 1] - frameTimestamps[0];
if (timeSpan > 0) {
const currentFps = Math.round((frameTimestamps.length - 1) * 1000 / timeSpan);
setFps(Math.max(0, currentFps)); // Ensure FPS is never negative
}
}
setFrameCount(prev => prev + 1);
setLastFrameTime(now);
// Create blob from JPEG data
const blob = new Blob([jpegData], { type: 'image/jpeg' });
const imageUrl = URL.createObjectURL(blob);
// Create image element
const img = new Image();
img.onload = () => {
const { drawX, drawY, drawWidth, drawHeight } = drawInfo();
// Clear canvas
_ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw image
_ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);
// Clean up
URL.revokeObjectURL(imageUrl);
};
img.onerror = () => {
console.error('Failed to load JPEG image');
URL.revokeObjectURL(imageUrl);
};
img.src = imageUrl;
} catch (error) {
console.error('Error handling JPEG frame:', error);
}
};
const resetSize = () => {
// Set canvas size first
canvas.width = root.clientWidth;
canvas.height = root.clientHeight;
// Use a default aspect ratio if canvas dimensions are invalid
const aspectRatio = (canvas.width > 0 && canvas.height > 0)
? canvas.width / canvas.height
: 16 / 9; // Default 16:9 aspect ratio
const drawWidth = Math.round(
Math.min(root.clientWidth, root.clientHeight * aspectRatio),
);
const drawHeight = Math.round(
Math.min(root.clientHeight, root.clientWidth / aspectRatio),
);
const drawX = Math.round((root.clientWidth - drawWidth) / 2);
const drawY = Math.round((root.clientHeight - drawHeight) / 2);
setDrawInfo({
drawX,
drawY,
drawWidth,
drawHeight,
});
};
const disconnect = () => {
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
if (websocket) {
websocket.close();
websocket = null;
}
};
// Initialize canvas and resize observer
onMount(() => {
console.log('ScreenViewWebSocket mounted with displayId:', localProps.displayId);
const context = canvas.getContext('2d');
setCtx(context);
// Initial size setup
resetSize();
const resizeObserver = new ResizeObserver(() => {
resetSize();
});
resizeObserver.observe(root);
// Connect WebSocket
console.log('About to connect WebSocket...');
connectWebSocket();
onCleanup(() => {
isMounted = false;
disconnect();
resizeObserver?.unobserve(root);
});
});
// Debug function to list displays
const debugDisplays = async () => {
try {
const result = await invoke('list_display_info');
console.log('Available displays:', result);
alert(`Available displays: ${result}`);
} catch (error) {
console.error('Failed to get display info:', error);
alert(`Error: ${error}`);
}
};
// Status indicator
const getStatusColor = () => {
switch (connectionStatus()) {
case 'connected': return '#10b981'; // green
case 'connecting': return '#f59e0b'; // yellow
case 'error': return '#ef4444'; // red
default: return '#6b7280'; // gray
}
};
return (
<div
ref={root!}
{...rootProps}
class={'overflow-hidden h-full w-full relative ' + (rootProps.class || '')}
>
<canvas
ref={canvas!}
style={{
display: 'block',
width: '100%',
height: '100%',
'background-color': '#f0f0f0'
}}
/>
{/* Status indicator */}
<div class="absolute top-2 right-2 flex items-center gap-2 bg-black bg-opacity-50 text-white px-2 py-1 rounded text-xs">
<div
class="w-2 h-2 rounded-full"
style={{ 'background-color': getStatusColor() }}
/>
<span>{connectionStatus()}</span>
{connectionStatus() === 'connected' && (
<span>| {fps()} FPS | {frameCount()} frames</span>
)}
<button
onClick={debugDisplays}
class="ml-2 px-1 py-0.5 bg-blue-600 hover:bg-blue-700 rounded text-xs"
title="Debug: Show available displays"
>
Debug
</button>
</div>
{rootProps.children}
</div>
);
};

View File

@ -1,4 +1,3 @@
import { convertFileSrc } from '@tauri-apps/api/tauri';
import {
Component,
createEffect,
@ -8,16 +7,29 @@ import {
onMount,
splitProps,
} from 'solid-js';
import { ScreenViewWebSocket } from './screen-view-websocket';
type ScreenViewProps = {
displayId: number;
useWebSocket?: boolean;
} & JSX.HTMLAttributes<HTMLDivElement>;
export const ScreenView: Component<ScreenViewProps> = (props) => {
const [localProps, rootProps] = splitProps(props, ['displayId']);
const [localProps, rootProps] = splitProps(props, ['displayId', 'useWebSocket']);
// Use WebSocket by default for better performance
if (localProps.useWebSocket !== false) {
return <ScreenViewWebSocket displayId={localProps.displayId} {...rootProps} />;
}
// Fallback to HTTP polling (legacy mode)
let canvas: HTMLCanvasElement;
let root: HTMLDivElement;
const [ctx, setCtx] = createSignal<CanvasRenderingContext2D | null>(null);
// Cache temporary canvas for scaling
let tempCanvas: HTMLCanvasElement | null = null;
let tempCtx: CanvasRenderingContext2D | null = null;
const [drawInfo, setDrawInfo] = createSignal({
drawX: 0,
drawY: 0,
@ -30,9 +42,84 @@ export const ScreenView: Component<ScreenViewProps> = (props) => {
height: number;
} | null>(null);
const [hidden, setHidden] = createSignal(false);
const [isLoading, setIsLoading] = createSignal(false);
let isMounted = true;
// Fetch screenshot data from backend with frame-based rendering
const fetchScreenshot = async () => {
if (isLoading()) {
return; // Skip if already loading - frame-based approach
}
try {
setIsLoading(true);
const timestamp = Date.now();
const response = await fetch(`ambient-light://displays/${localProps.displayId}?width=400&height=225&t=${timestamp}`);
if (!response.ok) {
console.error('Screenshot fetch failed:', response.status);
return;
}
const width = parseInt(response.headers.get('X-Image-Width') || '400');
const height = parseInt(response.headers.get('X-Image-Height') || '225');
const arrayBuffer = await response.arrayBuffer();
const buffer = new Uint8ClampedArray(arrayBuffer);
const expectedSize = width * height * 4;
// Validate buffer size
if (buffer.length !== expectedSize) {
console.error('Invalid buffer size:', buffer.length, 'expected:', expectedSize);
return;
}
setImageData({
buffer,
width,
height
});
// Draw immediately after data is set
setTimeout(() => {
draw(false);
}, 0);
// Frame-based rendering: wait for current frame to complete before scheduling next
const shouldContinue = !hidden() && isMounted;
if (shouldContinue) {
setTimeout(() => {
if (isMounted) {
fetchScreenshot(); // Start next frame only after current one completes
}
}, 500); // Reduced frequency to 500ms for better performance
}
} catch (error) {
console.error('Error fetching screenshot:', error);
// On error, wait longer before retry
const shouldContinueOnError = !hidden() && isMounted;
if (shouldContinueOnError) {
setTimeout(() => {
if (isMounted) {
fetchScreenshot();
}
}, 2000);
}
} finally {
setIsLoading(false);
}
};
const resetSize = () => {
const aspectRatio = canvas.width / canvas.height;
// Set canvas size first
canvas.width = root.clientWidth;
canvas.height = root.clientHeight;
// Use a default aspect ratio if canvas dimensions are invalid
const aspectRatio = (canvas.width > 0 && canvas.height > 0)
? canvas.width / canvas.height
: 16 / 9; // Default 16:9 aspect ratio
const drawWidth = Math.round(
Math.min(root.clientWidth, root.clientHeight * aspectRatio),
@ -51,106 +138,83 @@ export const ScreenView: Component<ScreenViewProps> = (props) => {
drawHeight,
});
canvas.width = root.clientWidth;
canvas.height = root.clientHeight;
draw(true);
};
const draw = (cached: boolean = false) => {
const { drawX, drawY } = drawInfo();
const { drawX, drawY, drawWidth, drawHeight } = drawInfo();
let _ctx = ctx();
let raw = imageData();
if (_ctx && raw) {
_ctx.clearRect(0, 0, canvas.width, canvas.height);
// Apply transparency effect for cached images if needed
let buffer = raw.buffer;
if (cached) {
for (let i = 3; i < raw.buffer.length; i += 8) {
raw.buffer[i] = Math.floor(raw.buffer[i] * 0.7);
buffer = new Uint8ClampedArray(raw.buffer);
for (let i = 3; i < buffer.length; i += 4) {
buffer[i] = Math.floor(buffer[i] * 0.7);
}
}
const img = new ImageData(raw.buffer, raw.width, raw.height);
_ctx.putImageData(img, drawX, drawY);
try {
// Create ImageData and draw directly
const img = new ImageData(buffer, raw.width, raw.height);
// If the image size matches the draw size, use putImageData directly
if (raw.width === drawWidth && raw.height === drawHeight) {
_ctx.putImageData(img, drawX, drawY);
} else {
// Otherwise, use cached temporary canvas for scaling
if (!tempCanvas || tempCanvas.width !== raw.width || tempCanvas.height !== raw.height) {
tempCanvas = document.createElement('canvas');
tempCanvas.width = raw.width;
tempCanvas.height = raw.height;
tempCtx = tempCanvas.getContext('2d');
}
if (tempCtx) {
tempCtx.putImageData(img, 0, 0);
_ctx.drawImage(tempCanvas, drawX, drawY, drawWidth, drawHeight);
}
}
} catch (error) {
console.error('Error in draw():', error);
}
}
};
// get screenshot
createEffect(() => {
let stopped = false;
const frame = async () => {
const { drawWidth, drawHeight } = drawInfo();
const url = convertFileSrc(
`displays/${localProps.displayId}?width=${drawWidth}&height=${drawHeight}`,
'ambient-light',
);
await fetch(url, {
mode: 'cors',
})
.then((res) => res.body?.getReader().read())
.then((buffer) => {
if (buffer?.value) {
setImageData({
buffer: new Uint8ClampedArray(buffer?.value),
width: drawWidth,
height: drawHeight,
});
} else {
setImageData(null);
}
draw();
});
};
(async () => {
while (!stopped) {
if (hidden()) {
await new Promise((resolve) => setTimeout(resolve, 1000));
continue;
}
await frame();
}
})();
onCleanup(() => {
stopped = true;
});
});
// resize
createEffect(() => {
let resizeObserver: ResizeObserver;
onMount(() => {
setCtx(canvas.getContext('2d'));
new ResizeObserver(() => {
resetSize();
}).observe(root);
});
// Initialize canvas and resize observer
onMount(() => {
const context = canvas.getContext('2d');
setCtx(context);
// Initial size setup
resetSize();
const resizeObserver = new ResizeObserver(() => {
resetSize();
});
resizeObserver.observe(root);
// Start screenshot fetching after context is ready
setTimeout(() => {
fetchScreenshot(); // Initial fetch - will self-schedule subsequent frames
}, 100); // Small delay to ensure context is ready
onCleanup(() => {
isMounted = false; // Stop scheduling new frames
resizeObserver?.unobserve(root);
});
});
// update hidden
createEffect(() => {
const hide = () => {
setHidden(true);
console.log('hide');
};
const show = () => {
setHidden(false);
console.log('show');
};
window.addEventListener('focus', show);
window.addEventListener('blur', hide);
onCleanup(() => {
window.removeEventListener('focus', show);
window.removeEventListener('blur', hide);
});
});
// Note: Removed window focus/blur logic as it was causing screenshot loop to stop
// when user interacted with dev tools or other windows
return (
<div
@ -158,7 +222,15 @@ export const ScreenView: Component<ScreenViewProps> = (props) => {
{...rootProps}
class={'overflow-hidden h-full w-full ' + rootProps.class}
>
<canvas ref={canvas!} />
<canvas
ref={canvas!}
style={{
display: 'block',
width: '100%',
height: '100%',
'background-color': '#f0f0f0'
}}
/>
{rootProps.children}
</div>
);

View File

@ -14,7 +14,7 @@ export const ColorSlider: Component<Props> = (props) => {
step={0.01}
value={props.value}
class={
'w-full h-2 bg-gradient-to-r rounded-lg appearance-none cursor-pointer dark:bg-gray-700 drop-shadow ' +
'range range-primary w-full bg-gradient-to-r ' +
props.class
}
/>

View File

@ -1,24 +1,101 @@
import { listen } from '@tauri-apps/api/event';
import { Component, createEffect, onCleanup } from 'solid-js';
import { Component, createEffect, onCleanup, createSignal } from 'solid-js';
import { ColorCalibration, LedStripConfigContainer } from '../../models/led-strip-config';
import { ledStripStore, setLedStripStore } from '../../stores/led-strip.store';
import { ColorSlider } from './color-slider';
import { TestColorsBg } from './test-colors-bg';
import { invoke } from '@tauri-apps/api';
import { invoke } from '@tauri-apps/api/core';
import { VsClose } from 'solid-icons/vs';
import { BiRegularReset } from 'solid-icons/bi';
import { BsFullscreen, BsFullscreenExit } from 'solid-icons/bs';
import { getCurrentWindow } from '@tauri-apps/api/window';
import transparentBg from '../../assets/transparent-grid-background.svg?url';
const Value: Component<{ value: number }> = (props) => {
return (
<span class="w-10 text-sm block font-mono text-right ">
{(props.value * 100).toFixed(0)}
<span class="text-xs text-stone-600">%</span>
</span>
<div class="badge badge-outline badge-sm font-mono">
{(props.value * 100).toFixed(0)}%
</div>
);
};
export const WhiteBalance = () => {
const [isFullscreen, setIsFullscreen] = createSignal(false);
const [panelPosition, setPanelPosition] = createSignal({ x: 0, y: 0 });
const [isDragging, setIsDragging] = createSignal(false);
const [dragOffset, setDragOffset] = createSignal({ x: 0, y: 0 });
// 自动进入全屏模式
createEffect(() => {
const autoEnterFullscreen = async () => {
try {
const window = getCurrentWindow();
const currentFullscreen = await window.isFullscreen();
if (!currentFullscreen) {
await window.setFullscreen(true);
setIsFullscreen(true);
} else {
setIsFullscreen(true);
}
} catch (error) {
console.error('Failed to auto enter fullscreen:', error);
}
};
autoEnterFullscreen();
});
// 初始化面板位置到屏幕中央
createEffect(() => {
if (isFullscreen()) {
const centerX = window.innerWidth / 2 - 160; // 160是面板宽度的一半
const centerY = window.innerHeight / 2 - 200; // 200是面板高度的一半
setPanelPosition({ x: centerX, y: centerY });
}
});
// 拖拽处理函数
const handleMouseDown = (e: MouseEvent) => {
setIsDragging(true);
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
setDragOffset({
x: e.clientX - rect.left,
y: e.clientY - rect.top
});
e.preventDefault();
};
const handleMouseMove = (e: MouseEvent) => {
if (isDragging()) {
const newX = e.clientX - dragOffset().x;
const newY = e.clientY - dragOffset().y;
// 限制面板在屏幕范围内
const maxX = window.innerWidth - 320; // 320是面板宽度
const maxY = window.innerHeight - 400; // 400是面板高度
setPanelPosition({
x: Math.max(0, Math.min(newX, maxX)),
y: Math.max(0, Math.min(newY, maxY))
});
}
};
const handleMouseUp = () => {
setIsDragging(false);
};
// 添加全局鼠标事件监听
createEffect(() => {
if (isDragging()) {
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
} else {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
}
});
// listen to config_changed event
createEffect(() => {
const unlisten = listen('config_changed', (event) => {
@ -32,20 +109,48 @@ export const WhiteBalance = () => {
});
});
onCleanup(() => {
unlisten.then((unlisten) => unlisten());
onCleanup(async () => {
(await unlisten)();
});
});
const updateColorCalibration = (field: keyof ColorCalibration, value: number) => {
const calibration = { ...ledStripStore.colorCalibration, [field]: value };
invoke('set_color_calibration', {
calibration,
}).catch((error) => console.log(error));
const updateColorCalibration = (
key: keyof ColorCalibration,
value: number,
) => {
const calibration = { ...ledStripStore.colorCalibration };
calibration[key] = value;
setLedStripStore('colorCalibration', calibration);
invoke('set_color_calibration', { calibration }).catch((error) =>
console.log(error),
);
};
const toggleFullscreen = async () => {
try {
const window = getCurrentWindow();
const currentFullscreen = await window.isFullscreen();
await window.setFullscreen(!currentFullscreen);
setIsFullscreen(!currentFullscreen);
// 退出全屏时重置面板位置
if (currentFullscreen) {
setPanelPosition({ x: 0, y: 0 });
}
} catch (error) {
console.error('Failed to toggle fullscreen:', error);
}
};
const exit = () => {
window.history.back();
// 退出时确保退出全屏模式
if (isFullscreen()) {
toggleFullscreen().then(() => {
window.history.back();
});
} else {
window.history.back();
}
};
const reset = () => {
@ -55,77 +160,273 @@ export const WhiteBalance = () => {
};
return (
<section class="select-none text-stone-800">
<div
class="absolute top-0 left-0 right-0 bottom-0"
style={{
'background-image': `url(${transparentBg})`,
}}
>
<TestColorsBg />
</div>
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-10/12 max-w-lg bg-stone-100/20 backdrop-blur p-5 rounded-xl shadow-lg">
<label class="flex items-center gap-2">
<span class="w-3 block">R:</span>
<ColorSlider
class="from-cyan-500 to-red-500"
value={ledStripStore.colorCalibration.r}
onInput={(ev) =>
updateColorCalibration(
'r',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
<Value value={ledStripStore.colorCalibration.r} />
</label>
<label class="flex items-center gap-2">
<span class="w-3 block">G:</span>
<ColorSlider
class="from-pink-500 to-green-500"
value={ledStripStore.colorCalibration.g}
onInput={(ev) =>
updateColorCalibration(
'g',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
<Value value={ledStripStore.colorCalibration.g} />
</label>
<label class="flex items-center gap-2">
<span class="w-3 block">B:</span>
<ColorSlider
class="from-yellow-500 to-blue-500"
value={ledStripStore.colorCalibration.b}
onInput={(ev) =>
updateColorCalibration(
'b',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
<Value value={ledStripStore.colorCalibration.b} />
</label>
<label class="flex items-center gap-2">
<span class="w-3 block">W:</span>
<ColorSlider class="from-yellow-50 to-cyan-50" />
</label>
<button
class="absolute -right-4 -top-4 rounded-full aspect-square bg-stone-100/20 backdrop-blur p-1 shadow hover:bg-stone-200/20 active:bg-stone-300"
onClick={exit}
title="Go Back"
>
<VsClose size={24} />
</button>
<button
class="absolute -right-4 -bottom-4 rounded-full aspect-square bg-stone-100/20 backdrop-blur p-1 shadow hover:bg-stone-200/20 active:bg-stone-300"
onClick={reset}
title="Reset to 100%"
>
<BiRegularReset size={24} />
</button>
</div>
</section>
<>
{/* 普通模式 */}
{!isFullscreen() && (
<div class="space-y-6">
<div class="flex items-center justify-between">
<h1 class="text-2xl font-bold text-base-content"></h1>
<div class="flex gap-2">
<button class="btn btn-outline btn-sm" onClick={toggleFullscreen} title="进入全屏">
<BsFullscreen size={16} />
</button>
<button class="btn btn-outline btn-sm" onClick={reset} title="重置到100%">
<BiRegularReset size={16} />
</button>
<button class="btn btn-primary btn-sm" onClick={exit} title="返回">
<VsClose size={16} />
</button>
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
{/* 颜色测试区域 */}
<div class="card bg-base-200 shadow-lg">
<div class="card-body p-4">
<div class="card-title text-base mb-3">
<span></span>
<div class="badge badge-info badge-outline"></div>
</div>
<div
class="aspect-square rounded-lg overflow-hidden border border-base-300"
style={{
'background-image': `url(${transparentBg})`,
}}
>
<TestColorsBg />
</div>
<div class="text-xs text-base-content/50 mt-2">
💡
</div>
</div>
</div>
{/* 白平衡控制面板 */}
<div class="card bg-base-200 shadow-lg">
<div class="card-body p-4">
<div class="card-title text-base mb-3">
<span>RGB调节</span>
<div class="badge badge-secondary badge-outline"></div>
</div>
<div class="space-y-4">
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-red-500"> (R)</span>
<Value value={ledStripStore.colorCalibration.r} />
</label>
<ColorSlider
class="from-cyan-500 to-red-500"
value={ledStripStore.colorCalibration.r}
onInput={(ev) =>
updateColorCalibration(
'r',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-green-500">绿 (G)</span>
<Value value={ledStripStore.colorCalibration.g} />
</label>
<ColorSlider
class="from-pink-500 to-green-500"
value={ledStripStore.colorCalibration.g}
onInput={(ev) =>
updateColorCalibration(
'g',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-blue-500"> (B)</span>
<Value value={ledStripStore.colorCalibration.b} />
</label>
<ColorSlider
class="from-yellow-500 to-blue-500"
value={ledStripStore.colorCalibration.b}
onInput={(ev) =>
updateColorCalibration(
'b',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-base-content/70"> (W)</span>
<div class="badge badge-outline badge-sm"></div>
</label>
<ColorSlider class="from-yellow-50 to-cyan-50" disabled />
</div>
</div>
{/* 使用说明 - 可展开 */}
<div class="collapse collapse-arrow bg-base-100 mt-4">
<input type="checkbox" />
<div class="collapse-title text-sm font-medium text-base-content/80">
💡 使
</div>
<div class="collapse-content text-xs text-base-content/70 space-y-3">
<div class="space-y-2">
<p class="font-semibold text-primary">🎯 使</p>
<ol class="list-decimal list-inside space-y-1 ml-2">
<li>"全屏"</li>
<li></li>
<li>RGB控制面板拖拽到合适位置</li>
<li>LED灯条颜色与屏幕边缘颜色</li>
</ol>
</div>
<div class="space-y-2">
<p class="font-semibold text-secondary">🔧 </p>
<ul class="list-disc list-inside space-y-1 ml-2">
<li><span class="text-red-500 font-medium"></span>R值LED会减少红色成分</li>
<li><span class="text-green-500 font-medium">绿</span>G值LED会减少绿色成分</li>
<li><span class="text-blue-500 font-medium"></span>B值LED会减少蓝色成分</li>
<li><span class="text-base-content font-medium"></span>B值R/G值</li>
<li><span class="text-base-content font-medium"></span>B值R/G值</li>
</ul>
</div>
<div class="space-y-2">
<p class="font-semibold text-accent">📋 </p>
<ul class="list-disc list-inside space-y-1 ml-2">
<li>LED白光与屏幕白色一致</li>
<li>LED颜色饱和度合适</li>
<li></li>
<li>"重置"</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
)}
{/* 全屏模式 */}
{isFullscreen() && (
<div class="fixed inset-0 w-screen h-screen bg-black z-50">
{/* 全屏颜色测试区域 - 紧贴边缘 */}
<div class="absolute inset-0 w-full h-full">
<TestColorsBg />
</div>
{/* 可拖拽的RGB控制面板 */}
<div
class="fixed w-80 bg-base-200/95 backdrop-blur-sm rounded-lg shadow-xl z-60 cursor-move select-none"
style={{
left: `${panelPosition().x}px`,
top: `${panelPosition().y}px`,
transform: 'none'
}}
onMouseDown={handleMouseDown}
>
<div class="card-body p-4">
<div class="card-title text-base mb-3 flex justify-between items-center">
<div class="flex items-center gap-2">
<span class="text-xs opacity-60"></span>
<span>RGB调节</span>
<div class="badge badge-secondary badge-outline"></div>
</div>
<button class="btn btn-ghost btn-xs" onClick={toggleFullscreen} title="退出全屏">
<BsFullscreenExit size={14} />
</button>
</div>
<div class="space-y-4">
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-red-500"> (R)</span>
<Value value={ledStripStore.colorCalibration.r} />
</label>
<ColorSlider
class="from-cyan-500 to-red-500"
value={ledStripStore.colorCalibration.r}
onInput={(ev) =>
updateColorCalibration(
'r',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-green-500">绿 (G)</span>
<Value value={ledStripStore.colorCalibration.g} />
</label>
<ColorSlider
class="from-pink-500 to-green-500"
value={ledStripStore.colorCalibration.g}
onInput={(ev) =>
updateColorCalibration(
'g',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-blue-500"> (B)</span>
<Value value={ledStripStore.colorCalibration.b} />
</label>
<ColorSlider
class="from-yellow-500 to-blue-500"
value={ledStripStore.colorCalibration.b}
onInput={(ev) =>
updateColorCalibration(
'b',
(ev.target as HTMLInputElement).valueAsNumber ?? 1,
)
}
/>
</div>
<div class="form-control">
<label class="label">
<span class="label-text font-semibold text-base-content/70"> (W)</span>
<div class="badge badge-outline badge-sm"></div>
</label>
<ColorSlider class="from-yellow-50 to-cyan-50" disabled />
</div>
</div>
<div class="text-xs text-base-content/60 mt-3 p-2 bg-base-300/50 rounded">
💡 LED灯条RGB滑块使颜色一致
</div>
<div class="flex gap-2 mt-4">
<button class="btn btn-outline btn-sm flex-1" onClick={reset} title="重置到100%">
<BiRegularReset size={14} />
</button>
<button class="btn btn-primary btn-sm flex-1" onClick={exit} title="返回">
<VsClose size={14} />
</button>
</div>
</div>
</div>
</div>
)}
</>
);
};
};

View File

@ -1,3 +1,2 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@import "tailwindcss";
@config "../tailwind.config.js";

View File

@ -1,9 +1,20 @@
import daisyui from 'daisyui';
/** @type {import('tailwindcss').Config} */
module.exports = {
mode: 'jit',
export default {
content: ['./src/**/*.{js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [],
plugins: [daisyui],
daisyui: {
themes: ["dark", "light"],
darkTheme: "dark",
base: true,
styled: true,
utils: true,
prefix: "",
logs: true,
themeRoot: ":root",
},
};

View File

@ -6,8 +6,14 @@ const mobile =
process.env.TAURI_PLATFORM === "ios";
// https://vitejs.dev/config/
export default defineConfig(async () => ({
plugins: [solidPlugin()],
export default defineConfig(async () => {
const tailwindcss = (await import("@tailwindcss/vite")).default;
return {
plugins: [
solidPlugin(),
tailwindcss(),
],
// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
// prevent vite from obscuring rust errors
@ -28,4 +34,5 @@ export default defineConfig(async () => ({
// produce sourcemaps for debug builds
sourcemap: !!process.env.TAURI_DEBUG,
},
}));
};
});