DioxusLabs / DioxusLabs/dioxus
Launcher configuration documentation
- Dominant language
- Rust
- Stars
- 39.1k
- Forks
- 1.9k
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 4
Description
Since I had trouble finding examples/documentation to fix the missing icon issue in the desktop version, I wrote this guide on configuring the launcher. I apologize if it's already there. Bye.
# Dioxus Desktop Launcher Configuration Guide
## Overview
This guide explains how to configure Dioxus desktop application launchers to customize the appearance and behavior of windows, including icons, dimensions, menus, and other advanced options.
## LaunchBuilder vs Simple Launch
### Simple Method
```rust
fn main() {
launch(App); // Default configuration
}
```
### Advanced Method with LaunchBuilder
```rust
fn main() {
LaunchBuilder::new()
.with_cfg(dioxus::desktop::Config::new()
.with_window(/* window configuration */)
/* other configurations */
)
.launch(App);
}
```
## Window Configuration
### WindowBuilder Options
```rust
use dioxus::prelude::*;
use dioxus::desktop::{Config, WindowBuilder, LogicalSize};
LaunchBuilder::new()
.with_cfg(dioxus::desktop::Config::new()
.with_window(
WindowBuilder::new()
.with_title("Application Title")
.with_inner_size(LogicalSize::new(800.0, 600.0))
.with_min_inner_size(Some(LogicalSize::new(400.0, 300.0)))
.with_max_inner_size(Some(LogicalSize::new(1920.0, 1080.0)))
.with_resizable(true)
.with_maximized(false)
.with_fullscreen(false)
.with_decorations(true) // Window borders
.with_transparent(false)
.with_always_on_top(false)
.with_visible(true)
.with_window_icon(Some(icon_data))
)
)
.launch(App);
```
### Option Descriptions
- **`with_title()`**: Window title
- **`with_inner_size()`**: Initial dimensions (width, height)
- **`with_min_inner_size()`**: Minimum resizable dimensions
- **`with_max_inner_size()`**: Maximum resizable dimensions
- **`with_resizable()`**: Whether the window can be resized
- **`with_maximized()`**: Whether the window starts maximized
- **`with_fullscreen()`**: Whether the window starts in fullscreen mode
- **`with_decorations()`**: Show/hide borders and title bar
- **`with_transparent()`**: Enable window transparency
- **`with_always_on_top()`**: Keep window always on top
- **`with_visible()`**: Control initial visibility
## Icon Management
### Window and Taskbar Icons
```rust
fn load_window_icon() -> Option {
// Load PNG icon and convert to RGBA
if let Ok(icon_bytes) = std::fs::read("icons/icon.png") {
if let Ok(img) = image::load_from_memory(&icon_bytes) {
let rgba_img = img.to_rgba8();
let (width, height) = rgba_img.dimensions();
let rgba_data = rgba_img.into_raw();
if let Ok(icon) = dioxus::desktop::tao::window::Icon::from_rgba(rgba_data, width, height) {
return Some(icon);
}
}
}
None
}
fn main() {
let window_icon = load_window_icon();
LaunchBuilder::new()
.with_cfg(dioxus::desktop::Config::new()
.with_window(
WindowBuilder::new()
.with_title("HotDog")
.with_window_icon(window_icon)
)
)
.launch(App);
}
```
### Required Dependencies
Add to `Cargo.toml`:
```toml
[dependencies]
image = { version = "0.24.0", features = ["png"] }
```
### Supported Icon Formats
- **PNG**: Recommended for transparency support
- **ICO**: Supported but requires conversion
- **JPEG/OTHER**: Supported via conversion
### Recommended Icon Sizes
- **Windows**: 32x32, 64x64, 256x256 pixels
- **macOS**: 16x16, 32x32, 128x128, 256x256, 512x512 pixels
- **Linux**: 32x32, 64x64 pixels
## Advanced Configurations
### Custom Protocols
```rust
fn handle_custom_protocol(request: &dioxus::desktop::AssetRequest) -> Option> {
match request.path() {
"/custom-resource" => Some(b"Custom content".to_vec()),
path => std::fs::read(path.strip_prefix('/').unwrap_or(path)).ok()
}
}
LaunchBuilder::new()
.with_cfg(dioxus::desktop::Config::new()
.with_custom_protocol("myapp", handle_custom_protocol)
)
.launch(App);
```
### Context Menu
```rust
LaunchBuilder::new()
.with_cfg(dioxus::desktop::Config::new()
.with_disable_context_menu(false) // false = enable default menu
)
.launch(App);
```
### Custom Menu
```rust
use dioxus::desktop::muda::{Menu, Submenu, MenuItem};
fn app() -> Element {
let window = window();
use_effect(move || {
let menu = Menu::new();
let file_menu = Submenu::new("File", true);
let quit = MenuItem::new("Quit", true, None);
file_menu.append(&quit);
menu.append(&file_menu);
window.set_menu(Some(menu));
});
// ... rest of component
}
```
## Multi-Window Configuration
```rust
fn app() -> Element {
let window = window();
rsx! {
button {
onclick: move |_| {
let dom = VirtualDom::new(popup_window);
window.open_window(dom, Default::default());
},
"Open Popup"
}
}
}
fn popup_window() -> Element {
rsx! {
div { "This is a popup window!" }
}
}
```
## Best Practices
### 1. Icon Error Handling
```rust
fn load_window_icon_safe() -> Option {
match std::fs::read("icons/icon.png") {
Ok(icon_bytes) => {
match image::load_from_memory(&icon_bytes) {
Ok(img) => {
let rgba_img = img.to_rgba8();
let (width, height) = rgba_img.dimensions();
let rgba_data = rgba_img.into_raw();
match dioxus::desktop::tao::window::Icon::from_rgba(rgba_data, width, height) {
Ok(icon) => {
tracing::info!("Icon loaded successfully: {}x{}", width, height);
Some(icon)
}
Err(e) => {
tracing::warn!("Failed to create icon: {}", e);
None
}
}
}
Err(e) => {
tracing::warn!("Failed to decode icon: {}", e);
None
}
}
}
Err(e) => {
tracing::warn!("Icon file not found: {}", e);
None
}
}
}
```
### 2. Conditional Configuration
```rust
fn main() {
let mut config = dioxus::desktop::Config::new();
// Basic window configuration
config = config.with_window(
WindowBuilder::new()
.with_title("HotDog")
.with_inner_size(LogicalSize::new(800.0, 600.0))
.with_resizable(true)
);
// Icon only if available
if let Some(icon) = load_window_icon_safe() {
config = config.with_window(
config.window().unwrap().clone()
.with_window_icon(Some(icon))
);
}
// Custom protocol only in debug mode
#[cfg(debug_assertions)]
{
config = config.with_custom_protocol("dev", handle_dev_protocol);
}
LaunchBuilder::new()
.with_cfg(config)
.launch(App);
}
```
### 3. Configuration Logging
```rust
use tracing::{info, warn};
fn main() {
info!("Initializing Dioxus Desktop launcher");
let window_icon = load_window_icon_safe();
if window_icon.is_none() {
warn!("No icon available for window");
}
LaunchBuilder::new()
.with_cfg(dioxus::desktop::Config::new()
.with_window(
WindowBuilder::new()
.with_title("HotDog")
.with_inner_size(LogicalSize::new(800.0, 600.0))
.with_resizable(true)
.with_window_icon(window_icon)
)
)
.launch(App);
info!("Application started successfully");
}
```
## Troubleshooting Common Issues
### Icon Not Appearing
1. Check icon file path
2. Ensure icon is in PNG/RGBA format
3. Verify dimensions (recommended 32x32 or 64x64)
4. Add logging to diagnose issues
### Window Not Resizable
- Check `with_resizable(true)`
- Verify minimum/maximum dimensions
### Menu Not Appearing
- Ensure `with_disable_context_menu(false)`
- Verify `muda` module import
## References
- [Dioxus Desktop Documentation](https://dioxuslabs.com/docs/0.4/guide/desktop/getting_started.html)
- [Tao Window Builder](https://docs.rs/tao/latest/tao/window/struct.WindowBuilder.html)
- [Image Crate Documentation](https://docs.rs/image/latest/image/)
---
**Note**: This guide refers to Dioxus 0.7.2 and compatible versions. APIs may change in future versions.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.