Skip to content

Commit

Permalink
PNG and SVG examples - Updated assets
Browse files Browse the repository at this point in the history
The original icon.png had unknown origins, prior to inclusion in winit.
Replaced with assets that were already in the repository and do not
require attribution per #116 (comment)
  • Loading branch information
thewizzy committed May 22, 2024
1 parent 3c9caf1 commit 737350f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
3 changes: 2 additions & 1 deletion examples/window-icon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
im.workspace = true
floem = { path = "../.." }
image = "0.25.1"
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.
Binary file removed examples/window-icon/assets/icon.png
Binary file not shown.
58 changes: 50 additions & 8 deletions examples/window-icon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
use floem::{
close_window,
event::{Event, EventListener},
keyboard::{Key, NamedKey},
kurbo::Size,
views::Decorators,
window::{Icon, WindowConfig},
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 = "Show Window Icon".style(|s| s.font_size(30.0)).style(|s| {
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()
Expand All @@ -29,23 +65,23 @@ fn app_view() -> impl IntoView {
}

fn main() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "./assets/icon.png");
let icon = load_icon(Path::new(path));
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 Size Example")
.window_icon(icon),
.title("Window Icon Example")
.window_icon(png_icon),
),
)
.run();
}

fn load_icon(path: &Path) -> Icon {
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")
Expand All @@ -56,3 +92,9 @@ fn load_icon(path: &Path) -> Icon {
};
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")
}

0 comments on commit 737350f

Please sign in to comment.