Skip to content

Commit

Permalink
Implemented theme icons.
Browse files Browse the repository at this point in the history
Theme icons are icon widgets that are created with only their name as arguments.
The global static `ICON_LOADER` then searches the currently used icon theme for an icon with that name.
If an icon with that name exists with multiple sizes in the theme, the icon with the best size for the widget is chosen.
  • Loading branch information
Maldela committed Dec 11, 2019
1 parent 71432cc commit e427508
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 40 deletions.
44 changes: 25 additions & 19 deletions examples/todos.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use iced::{
button, scrollable, text_input, Align, Application, Button, Checkbox,
Color, Column, Command, Container, Element, Font, HorizontalAlignment,
Length, Row, Scrollable, Settings, Text, TextInput,
Icon, Length, Row, Scrollable, Settings, Text, TextInput,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -291,12 +291,9 @@ impl Task {
.align_items(Align::Center)
.push(checkbox)
.push(
Button::new(
edit_button,
edit_icon().color([0.5, 0.5, 0.5]),
)
.on_press(TaskMessage::Edit)
.padding(10),
Button::new(edit_button, edit_icon())
.on_press(TaskMessage::Edit)
.padding(10),
)
.into()
}
Expand All @@ -320,14 +317,11 @@ impl Task {
.push(
Button::new(
delete_button,
Row::new()
.spacing(10)
.push(delete_icon().color(Color::WHITE))
.push(
Text::new("Delete")
.width(Length::Shrink)
.color(Color::WHITE),
),
Row::new().spacing(10).push(delete_icon()).push(
Text::new("Delete")
.width(Length::Shrink)
.color(Color::WHITE),
),
)
.on_press(TaskMessage::Delete)
.padding(10)
Expand Down Expand Up @@ -471,12 +465,24 @@ fn icon(unicode: char) -> Text {
.size(20)
}

fn edit_icon() -> Text {
icon('\u{F303}')
fn edit_icon<'a, Message>() -> Element<'a, Message> {
if Icon::theme_includes_icon("document-properties") {
Icon::from_theme("document-properties")
.size(Length::Units(32))
.into()
} else {
icon('\u{F303}').color([0.5, 0.5, 0.5]).into()
}
}

fn delete_icon() -> Text {
icon('\u{F1F8}')
fn delete_icon<'a, Message>() -> Element<'a, Message> {
if Icon::theme_includes_icon("edit-delete") {
Icon::from_theme("edit-delete")
.size(Length::Units(32))
.into()
} else {
icon('\u{F1F8}').color(Color::WHITE).into()
}
}

// Persistence
Expand Down
3 changes: 3 additions & 0 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ iced_core = { version = "0.1.0", path = "../core", features = ["command"] }
twox-hash = "1.5"
raw-window-handle = "0.3"
unicode-segmentation = "1.6"
xdg = "2.2"
rust-ini = "0.13"
lazy_static = "1.4"
5 changes: 5 additions & 0 deletions native/src/icon_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! A collection of structs to handle themed icons.
mod icon_loader;

pub use icon_loader::*;
Loading

0 comments on commit e427508

Please sign in to comment.