Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose setting a Window Icon #453

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/window-icon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "window-icon"
version = "0.1.0"
edition = "2021"

[dependencies]
im.workspace = true
floem = { path = "../.." }
image = "0.25"
nsvg = "0.5"
Binary file added examples/window-icon/assets/ferris.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/window-icon/assets/ferris.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions examples/window-icon/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use floem::{
close_window,
event::{Event, EventListener},
keyboard::{Key, NamedKey},
kurbo::Size,
new_window,
views::{button, label, v_stack, Decorators},
window::{Icon, WindowConfig, WindowId},
Application, IntoView, View,
};
use std::path::Path;

fn sub_window_view(id: WindowId) -> impl IntoView {
v_stack((
label(move || String::from("This window has an icon from an SVG file."))
.style(|s| s.font_size(30.0)),
button(|| "Close this window").on_click_stop(move |_| {
close_window(id);
}),
))
.style(|s| {
s.flex_col()
.items_center()
.justify_center()
.width_full()
.height_full()
.gap(0.0, 10.0)
})
}

fn app_view() -> impl IntoView {
let view = v_stack((
label(move || String::from("This window has an icon from a PNG file"))
.style(|s| s.font_size(30.0)),
button(|| "Open another window").on_click_stop(|_| {
let svg_icon = load_svg_icon(include_str!("../assets/ferris.svg"));
new_window(
sub_window_view,
Some(
WindowConfig::default()
.size(Size::new(600.0, 150.0))
.title("Window Icon Sub Example")
.window_icon(svg_icon),
),
);
}),
))
.style(|s| {
s.flex_col()
.items_center()
.justify_center()
.width_full()
.height_full()
.gap(0.0, 10.0)
});

let id = view.id();
view.on_event_stop(EventListener::KeyUp, move |e| {
if let Event::KeyUp(e) = e {
if e.key.logical_key == Key::Named(NamedKey::F11) {
id.inspect();
}
}
})
}

fn main() {
let png_icon_path = concat!(env!("CARGO_MANIFEST_DIR"), "./assets/ferris.png");
let png_icon = load_png_icon(Path::new(png_icon_path));

Application::new()
.window(
|_| app_view(),
Some(
WindowConfig::default()
.size(Size::new(800.0, 250.0))
.title("Window Icon Example")
.window_icon(png_icon),
),
)
.run();
}

fn load_png_icon(path: &Path) -> Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}

fn load_svg_icon(svg: &str) -> Icon {
let svg = nsvg::parse_str(svg, nsvg::Units::Pixel, 96.0).unwrap();
let (icon_width, icon_height, icon_rgba) = svg.rasterize_to_raw_rgba(1.0).unwrap();
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}
4 changes: 4 additions & 0 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,14 @@ impl ApplicationHandle {
if let Some(title) = config.title {
window_builder = window_builder.with_title(title);
}
if let Some(window_icon) = config.window_icon {
window_builder = window_builder.with_window_icon(Some(window_icon));
}
config.apply_default_theme.unwrap_or(true)
} else {
true
};

let result = window_builder.build(event_loop);
let window = match result {
Ok(window) => window,
Expand Down
5 changes: 3 additions & 2 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use floem_winit::window::Fullscreen;
pub use floem_winit::window::Icon;
pub use floem_winit::window::ResizeDirection;
pub use floem_winit::window::Theme;
pub use floem_winit::window::WindowButtons;
Expand All @@ -16,7 +17,7 @@ pub struct WindowConfig {
pub(crate) show_titlebar: Option<bool>,
pub(crate) transparent: Option<bool>,
pub(crate) fullscreen: Option<Fullscreen>,
pub(crate) window_icon: Option<bool>,
pub(crate) window_icon: Option<Icon>,
pub(crate) title: Option<String>,
pub(crate) enabled_buttons: Option<WindowButtons>,
pub(crate) resizable: Option<bool>,
Expand Down Expand Up @@ -50,7 +51,7 @@ impl WindowConfig {
self
}

pub fn window_icon(mut self, window_icon: bool) -> Self {
pub fn window_icon(mut self, window_icon: Icon) -> Self {
self.window_icon = Some(window_icon);
self
}
Expand Down