Skip to content

Commit 494f542

Browse files
committed
improv: menu item icons
1 parent 0003412 commit 494f542

File tree

12 files changed

+163
-44
lines changed

12 files changed

+163
-44
lines changed

Cargo.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Loading
Lines changed: 2 additions & 0 deletions
Loading
Lines changed: 2 additions & 0 deletions
Loading
Lines changed: 2 additions & 0 deletions
Loading

src/app.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::collections::HashMap;
66
use crate::calculation::Calculation;
77
use crate::config;
88
use crate::config::CONFIG_VERSION;
9+
use crate::core::{icons, key_binds::key_binds};
910
use crate::fl;
1011
use crate::operator::Operator;
1112
use cosmic::app::context_drawer;
@@ -19,7 +20,7 @@ use cosmic::iced::{
1920
Alignment, Event, Length, Subscription,
2021
};
2122
use cosmic::widget::about::About;
22-
use cosmic::widget::menu::Action;
23+
use cosmic::widget::menu::{Action, ItemHeight, ItemWidth};
2324
use cosmic::widget::{self, menu, nav_bar, ToastId};
2425
use cosmic::{cosmic_config, cosmic_theme, theme, Application, ApplicationExt, Element};
2526

@@ -59,14 +60,6 @@ pub enum ContextPage {
5960
About,
6061
}
6162

62-
impl ContextPage {
63-
fn _title(&self) -> String {
64-
match self {
65-
Self::About => fl!("about"),
66-
}
67-
}
68-
}
69-
7063
#[derive(Clone, Debug)]
7164
pub struct Flags {
7265
pub config_handler: Option<cosmic_config::Config>,
@@ -167,7 +160,7 @@ impl Application for Calculator {
167160
core,
168161
about,
169162
context_page: ContextPage::default(),
170-
key_binds: HashMap::new(),
163+
key_binds: key_binds(),
171164
nav,
172165
modifiers: Modifiers::empty(),
173166
config_handler: flags.config_handler,
@@ -191,11 +184,22 @@ impl Application for Calculator {
191184
menu::items(
192185
&self.key_binds,
193186
vec![
194-
menu::Item::Button(fl!("clear-history"), MenuAction::ClearHistory),
195-
menu::Item::Button(fl!("about"), MenuAction::About),
187+
menu::Item::Button(
188+
fl!("clear-history"),
189+
Some(icons::get_handle("large-brush-symbolic", 14)),
190+
MenuAction::ClearHistory,
191+
),
192+
menu::Item::Button(
193+
fl!("about"),
194+
Some(icons::get_handle("settings-symbolic", 14)),
195+
MenuAction::About,
196+
),
196197
],
197198
),
198-
)]);
199+
)])
200+
.item_height(ItemHeight::Dynamic(40))
201+
.item_width(ItemWidth::Uniform(240))
202+
.spacing(4.0);
199203

200204
vec![menu_bar.into()]
201205
}
@@ -208,6 +212,7 @@ impl Application for Calculator {
208212
&HashMap::new(),
209213
vec![cosmic::widget::menu::Item::Button(
210214
fl!("delete"),
215+
Some(icons::get_handle("user-trash-symbolic", 14)),
211216
NavMenuAction::Delete(id),
212217
)],
213218
))

src/core/icons.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
3+
use cosmic::widget::icon;
4+
use std::collections::HashMap;
5+
use std::sync::{Mutex, OnceLock};
6+
7+
pub(crate) static ICON_CACHE: OnceLock<Mutex<IconCache>> = OnceLock::new();
8+
9+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
10+
pub struct IconCacheKey {
11+
name: &'static str,
12+
size: u16,
13+
}
14+
15+
pub struct IconCache {
16+
cache: HashMap<IconCacheKey, icon::Handle>,
17+
}
18+
19+
impl IconCache {
20+
pub fn new() -> Self {
21+
let mut cache = HashMap::new();
22+
23+
macro_rules! bundle {
24+
($name:expr, $size:expr) => {
25+
let data: &'static [u8] =
26+
include_bytes!(concat!("../../res/icons/bundled/", $name, ".svg"));
27+
cache.insert(
28+
IconCacheKey {
29+
name: $name,
30+
size: $size,
31+
},
32+
icon::from_svg_bytes(data).symbolic(true),
33+
);
34+
};
35+
}
36+
37+
bundle!("large-brush-symbolic", 14);
38+
bundle!("info-outline-symbolic", 14);
39+
bundle!("settings-symbolic", 14);
40+
bundle!("user-trash-symbolic", 14);
41+
42+
Self { cache }
43+
}
44+
45+
pub fn get(&mut self, name: &'static str, size: u16) -> icon::Icon {
46+
let handle = self
47+
.cache
48+
.entry(IconCacheKey { name, size })
49+
.or_insert_with(|| icon::from_name(name).size(size).handle())
50+
.clone();
51+
icon::icon(handle).size(size)
52+
}
53+
54+
pub fn get_handle(&mut self, name: &'static str, size: u16) -> icon::Handle {
55+
let handle = self
56+
.cache
57+
.entry(IconCacheKey { name, size })
58+
.or_insert_with(|| icon::from_name(name).size(size).handle())
59+
.clone();
60+
handle
61+
}
62+
}
63+
64+
pub fn get_icon(name: &'static str, size: u16) -> icon::Icon {
65+
let mut icon_cache = ICON_CACHE.get().unwrap().lock().unwrap();
66+
icon_cache.get(name, size)
67+
}
68+
69+
pub fn get_handle(name: &'static str, size: u16) -> icon::Handle {
70+
let mut icon_cache = ICON_CACHE.get().unwrap().lock().unwrap();
71+
icon_cache.get_handle(name, size)
72+
}

src/core/key_binds.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashMap;
2+
3+
use cosmic::iced::keyboard::Key;
4+
use cosmic::widget::menu::key_bind::KeyBind;
5+
use cosmic::widget::menu::key_bind::Modifier;
6+
7+
use crate::app::MenuAction;
8+
9+
pub fn key_binds() -> HashMap<KeyBind, MenuAction> {
10+
let mut key_binds = HashMap::new();
11+
12+
macro_rules! bind {
13+
([$($modifier:ident),* $(,)?], $key:expr, $action:ident) => {{
14+
key_binds.insert(
15+
KeyBind {
16+
modifiers: vec![$(Modifier::$modifier),*],
17+
key: $key,
18+
},
19+
MenuAction::$action,
20+
);
21+
}};
22+
}
23+
24+
bind!([Ctrl, Shift], Key::Character("C".into()), ClearHistory);
25+
bind!([Ctrl], Key::Character("i".into()), About);
26+
27+
key_binds
28+
}

src/core/localization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn localizer() -> Box<dyn Localizer> {
3737
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
3838
}
3939

40-
pub fn set_localization() {
40+
pub fn localize() {
4141
let localizer = localizer();
4242
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
4343

src/core/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

3+
pub mod icons;
4+
pub mod key_binds;
35
pub mod localization;

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod operator;
99
mod settings;
1010

1111
fn main() -> cosmic::iced::Result {
12-
let (settings, flags) = settings::init();
12+
settings::init();
13+
let (settings, flags) = (settings::settings(), settings::flags());
1314
cosmic::app::run::<Calculator>(settings, flags)
1415
}

0 commit comments

Comments
 (0)