Skip to content

Commit 199b86f

Browse files
committed
improv: color schemes
- added view for empty color schemes - divided installed and available with tabs - removed `with_children` where possible - re-enabled wgpu feature to fix scroll perf - moved context dependent buttons to footer - made color schemes page responsive
1 parent 22f7d56 commit 199b86f

File tree

12 files changed

+703
-579
lines changed

12 files changed

+703
-579
lines changed

Cargo.lock

+191-233
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ chrono = { version = "0.4.38", features = ["serde"] }
2222
[dependencies.libcosmic]
2323
git = "https://github.com/pop-os/libcosmic.git"
2424
default-features = false
25-
features = ["tokio", "winit", "about"]
25+
features = ["tokio", "winit", "wgpu", "about"]
2626

2727
[dependencies.cosmic-ext-config-templates]
2828
git = "https://github.com/ryanabx/cosmic-ext-config-templates"

i18n/en/cosmic_ext_tweaks.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import-color-scheme = Import color scheme
1515
delete-color-scheme = Delete color scheme
1616
available-color-schemes-body = Find and install color schemes
1717
install-color-scheme = Install color scheme
18+
set-color-scheme = Set color scheme
1819
find-color-schemes = Find color schemes
1920
open-containing-folder = Open containing folder
2021
open-link = Open link
@@ -80,4 +81,4 @@ view = View
8081
## Shortcuts
8182

8283
warning = Warning: this will remove your existing custom shortcuts
83-
windows-desc = Super+Arrows to move windows. Ctrl+Alt+Arrows to navigate workspaces.
84+
windows-desc = Super+Arrows to move windows. Ctrl+Alt+Arrows to navigate workspaces.
Loading

src/app.rs

+80-141
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use cosmic::{
2121
Application, ApplicationExt, Apply, Element, Task,
2222
};
2323
use key_bind::key_binds;
24-
use pages::color_schemes::providers::cosmic_themes::CosmicTheme;
2524

2625
use crate::{
2726
app::{
@@ -32,7 +31,7 @@ use crate::{
3231
fl,
3332
pages::{
3433
self,
35-
color_schemes::{config::ColorScheme, preview, ColorSchemeProvider, ColorSchemes},
34+
color_schemes::{self, ColorSchemes, Status, Tab},
3635
dock::Dock,
3736
layouts::Layouts,
3837
panel::Panel,
@@ -67,23 +66,12 @@ pub struct TweakTool {
6766
app_themes: Vec<String>,
6867
config_handler: Option<cosmic_config::Config>,
6968
config: TweaksConfig,
70-
available: Vec<ColorScheme>,
71-
status: Status,
72-
limit: usize,
73-
offset: usize,
74-
}
75-
76-
pub enum Status {
77-
Idle,
78-
Loading,
79-
LoadingMore,
8069
}
8170

8271
#[derive(Clone, Debug, Eq, PartialEq)]
8372
pub enum DialogPage {
8473
SaveCurrentColorScheme(String),
8574
CreateSnapshot(String),
86-
AvailableColorSchemes,
8775
}
8876

8977
#[derive(Debug, Clone)]
@@ -102,8 +90,6 @@ pub enum Message {
10290
ToggleContextDrawer,
10391
ToggleDialogPage(DialogPage),
10492
AppTheme(usize),
105-
FetchAvailableColorSchemes(ColorSchemeProvider, usize),
106-
SetAvailableColorSchemes(Vec<ColorScheme>),
10793
Key(Modifiers, Key),
10894
Modifiers(Modifiers),
10995
SystemThemeModeChange,
@@ -213,18 +199,16 @@ impl Application for TweakTool {
213199
app_themes: vec![fl!("match-desktop"), fl!("dark"), fl!("light")],
214200
config_handler: flags.config_handler,
215201
config: flags.config,
216-
available: vec![],
217-
status: Status::Idle,
218-
limit: 15,
219-
offset: 0,
220202
shorcuts: Shortcuts::new(),
221203
};
222204

223205
let mut tasks = vec![
224-
app.update(Message::FetchAvailableColorSchemes(
225-
ColorSchemeProvider::CosmicThemes,
226-
app.limit,
227-
)),
206+
app.update(Message::ColorSchemes(Box::new(
207+
color_schemes::Message::FetchAvailableColorSchemes(
208+
color_schemes::ColorSchemeProvider::CosmicThemes,
209+
app.color_schemes.limit,
210+
),
211+
))),
228212
app.update(Message::Snapshots(
229213
pages::snapshots::Message::CreateSnapshot(
230214
fl!("application-opened"),
@@ -319,17 +303,17 @@ impl Application for TweakTool {
319303
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
320304
)
321305
.control(
322-
widget::column::with_children(vec![
323-
widget::text::body(fl!("color-scheme-name")).into(),
324-
widget::text_input("", name.as_str())
325-
.id(self.dialog_text_input.clone())
326-
.on_input(move |name| {
327-
Message::DialogUpdate(DialogPage::SaveCurrentColorScheme(name))
328-
})
329-
.on_submit(Message::DialogComplete)
330-
.into(),
331-
])
332-
.spacing(spacing.space_xxs),
306+
widget::column()
307+
.push(widget::text::body(fl!("color-scheme-name")))
308+
.push(
309+
widget::text_input("", name.as_str())
310+
.id(self.dialog_text_input.clone())
311+
.on_input(move |name| {
312+
Message::DialogUpdate(DialogPage::SaveCurrentColorScheme(name))
313+
})
314+
.on_submit(Message::DialogComplete),
315+
)
316+
.spacing(spacing.space_xxs),
333317
),
334318
DialogPage::CreateSnapshot(name) => widget::dialog()
335319
.title(fl!("create-snapshot"))
@@ -349,39 +333,6 @@ impl Application for TweakTool {
349333
})
350334
.on_submit(Message::DialogComplete),
351335
),
352-
DialogPage::AvailableColorSchemes => {
353-
let show_more_button: Option<Element<Message>> = match self.status {
354-
Status::Idle => Some(
355-
widget::button::text(fl!("show-more"))
356-
.on_press(Message::FetchAvailableColorSchemes(
357-
ColorSchemeProvider::CosmicThemes,
358-
self.limit,
359-
))
360-
.class(cosmic::style::Button::Standard)
361-
.into(),
362-
),
363-
Status::LoadingMore => Some(
364-
widget::button::text(fl!("loading"))
365-
.class(cosmic::style::Button::Standard)
366-
.into(),
367-
),
368-
Status::Loading => None,
369-
};
370-
371-
let mut dialog = widget::dialog()
372-
.title(fl!("available"))
373-
.body(fl!("available-color-schemes-body"))
374-
.secondary_action(
375-
widget::button::standard(fl!("close")).on_press(Message::DialogCancel),
376-
)
377-
.control(self.available_themes());
378-
379-
if let Some(show_more_button) = show_more_button {
380-
dialog = dialog.primary_action(show_more_button);
381-
}
382-
383-
dialog
384-
}
385336
};
386337

387338
Some(dialog.into())
@@ -405,14 +356,74 @@ impl Application for TweakTool {
405356
Page::Shortcuts => self.shorcuts.view().map(Message::Shortcuts),
406357
};
407358

408-
widget::column::with_children(vec![view])
359+
widget::column()
360+
.push(view)
409361
.padding(spacing.space_xs)
410362
.width(Length::Fill)
411363
.height(Length::Fill)
412364
.align_x(Alignment::Center)
413365
.into()
414366
}
415367

368+
fn footer(&self) -> Option<Element<Self::Message>> {
369+
let spacing = cosmic::theme::active().cosmic().spacing;
370+
371+
match self.nav_model.active_data::<Page>() {
372+
Some(Page::ColorSchemes) => match self.color_schemes.model.active_data::<Tab>() {
373+
Some(Tab::Installed) => Some(
374+
widget::row()
375+
.push(widget::horizontal_space())
376+
.push(
377+
widget::button::standard(fl!("save-current-color-scheme"))
378+
.trailing_icon(icons::get_handle("arrow-into-box-symbolic", 16))
379+
.on_press(Message::ColorSchemes(Box::new(
380+
color_schemes::Message::SaveCurrentColorScheme(None),
381+
))),
382+
)
383+
.push(
384+
widget::button::standard(fl!("import-color-scheme"))
385+
.trailing_icon(icons::get_handle("document-save-symbolic", 16))
386+
.on_press(Message::ColorSchemes(Box::new(
387+
color_schemes::Message::StartImport,
388+
))),
389+
)
390+
.spacing(spacing.space_xxs)
391+
.apply(widget::container)
392+
.class(cosmic::style::Container::Card)
393+
.padding(spacing.space_xxs)
394+
.into(),
395+
),
396+
Some(Tab::Available) => Some(
397+
widget::row()
398+
.push(widget::horizontal_space())
399+
.push(match self.color_schemes.status {
400+
Status::Idle => widget::button::standard(fl!("show-more"))
401+
.leading_icon(crate::core::icons::get_handle(
402+
"content-loading-symbolic",
403+
16,
404+
))
405+
.on_press(Message::ColorSchemes(Box::new(
406+
color_schemes::Message::FetchAvailableColorSchemes(
407+
color_schemes::ColorSchemeProvider::CosmicThemes,
408+
self.color_schemes.limit,
409+
),
410+
))),
411+
Status::LoadingMore | Status::Loading => {
412+
widget::button::standard(fl!("loading"))
413+
}
414+
})
415+
.spacing(spacing.space_xxs)
416+
.apply(widget::container)
417+
.class(cosmic::style::Container::Card)
418+
.padding(spacing.space_xxs)
419+
.into(),
420+
),
421+
None => None,
422+
},
423+
_ => None,
424+
}
425+
}
426+
416427
fn update(&mut self, message: Self::Message) -> cosmic::Task<app::Message<Self::Message>> {
417428
// Helper for updating config values efficiently
418429
macro_rules! config_set {
@@ -448,45 +459,6 @@ impl Application for TweakTool {
448459
log::error!("{err}")
449460
}
450461
}
451-
Message::FetchAvailableColorSchemes(provider, limit) => {
452-
if self.offset == 0 {
453-
self.status = Status::Loading;
454-
} else {
455-
self.status = Status::LoadingMore;
456-
}
457-
self.limit = limit;
458-
self.offset += self.limit;
459-
let limit = self.limit;
460-
let offset = self.offset;
461-
tasks.push(Task::perform(
462-
async move {
463-
let url = match provider {
464-
ColorSchemeProvider::CosmicThemes => {
465-
format!("https://cosmic-themes.org/api/themes/?order=name&limit={}&offset={}", limit, offset)
466-
}
467-
};
468-
469-
let response = reqwest::get(url).await?;
470-
let themes: Vec<CosmicTheme> = response.json().await?;
471-
let available = themes
472-
.into_iter()
473-
.map(ColorScheme::from)
474-
.collect();
475-
Ok(available)
476-
},
477-
|res: Result<Vec<ColorScheme>, reqwest::Error>| match res {
478-
Ok(themes) => cosmic::app::Message::App(Message::SetAvailableColorSchemes(themes)),
479-
Err(e) => {
480-
log::error!("{e}");
481-
cosmic::app::Message::App(Message::SetAvailableColorSchemes(vec![]))
482-
}
483-
},
484-
));
485-
}
486-
Message::SetAvailableColorSchemes(mut available) => {
487-
self.status = Status::Idle;
488-
self.available.append(&mut available);
489-
}
490462
Message::AppTheme(index) => {
491463
let app_theme = match index {
492464
1 => AppTheme::Dark,
@@ -535,9 +507,6 @@ impl Application for TweakTool {
535507
DialogPage::SaveCurrentColorScheme(String::new()),
536508
)))
537509
}
538-
pages::color_schemes::Message::OpenAvailableThemes => tasks.push(
539-
self.update(Message::ToggleDialogPage(DialogPage::AvailableColorSchemes)),
540-
),
541510
_ => tasks.push(
542511
self.color_schemes
543512
.update(*message)
@@ -569,7 +538,6 @@ impl Application for TweakTool {
569538
pages::snapshots::Message::CreateSnapshot(name, SnapshotKind::User),
570539
)))
571540
}
572-
DialogPage::AvailableColorSchemes => (),
573541
}
574542
}
575543
}
@@ -645,6 +613,7 @@ impl Application for TweakTool {
645613

646614
impl TweakTool {
647615
fn update_config(&mut self) -> Task<cosmic::app::Message<Message>> {
616+
self.color_schemes.refresh_theme_mode();
648617
app::command::set_theme(self.config.app_theme.theme())
649618
}
650619

@@ -666,34 +635,4 @@ impl TweakTool {
666635
.into()])
667636
.into()
668637
}
669-
670-
fn available_themes<'a>(&self) -> Element<'a, Message> {
671-
let spacing = cosmic::theme::active().cosmic().spacing;
672-
673-
let loading: Option<Element<'a, Message>> = if let Status::Loading = self.status {
674-
Some(widget::text(fl!("loading")).into())
675-
} else {
676-
None
677-
};
678-
679-
let available: Option<Element<'a, Message>> = match self.status {
680-
Status::Idle | Status::LoadingMore => {
681-
let themes: Vec<Element<Message>> =
682-
self.available.iter().map(preview::available).collect();
683-
let widgets = widget::flex_row(themes)
684-
.row_spacing(spacing.space_xs)
685-
.column_spacing(spacing.space_xs)
686-
.apply(widget::container)
687-
.padding([0, spacing.space_xxs]);
688-
Some(widgets.into())
689-
}
690-
Status::Loading => None,
691-
};
692-
693-
widget::container(widget::scrollable(widget::column::with_children(
694-
loading.into_iter().chain(available).collect(),
695-
)))
696-
.height(Length::Fixed(450.0))
697-
.into()
698-
}
699638
}

src/core/icons.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl IconCache {
5757
bundle!("list-add-symbolic", 16);
5858
bundle!("symbolic-link-symbolic", 14);
5959
bundle!("user-trash-symbolic", 14);
60+
bundle!("selection-mode-symbolic", 14);
6061
bundle!("folder-download-symbolic", 14);
6162
bundle!("arrow-circular-bottom-right-symbolic", 14);
6263

0 commit comments

Comments
 (0)