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

feat: high contrast toggle #1039

Merged
merged 1 commit into from
Mar 10, 2025
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
34 changes: 17 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions cosmic-settings/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,21 @@ impl cosmic::Application for SettingsApp {
.dialog(self.active_page)
.map(|e| e.map(Message::PageMessage))
}

fn system_theme_update(
&mut self,
_keys: &[&'static str],
new_theme: &cosmic::cosmic_theme::Theme,
) -> Task<Self::Message> {
if let Some(page) = self.pages.page_mut::<accessibility::Page>() {
page.update(accessibility::Message::SystemTheme(Box::new(
new_theme.clone(),
)))
.map(Into::into)
} else {
Task::none()
}
}
}

impl SettingsApp {
Expand Down
79 changes: 77 additions & 2 deletions cosmic-settings/src/pages/accessibility/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use cosmic::{
app,
cosmic_theme::{CosmicPalette, ThemeBuilder},
iced_core::text::Wrapping,
theme,
theme::{self, CosmicTheme},
widget::{button, container, horizontal_space, icon, settings, text},
Apply,
Apply, Task,
};
pub use cosmic_comp_config::ZoomMovement;
use cosmic_config::CosmicConfigEntry;
use cosmic_settings_page::{
self as page,
section::{self, Section},
Expand All @@ -14,6 +17,7 @@ use slotmap::SlotMap;

pub mod magnifier;
mod wayland;
use tokio::task::spawn_blocking;
pub use wayland::{AccessibilityEvent, AccessibilityRequest};

#[derive(Debug, Default)]
Expand All @@ -23,13 +27,17 @@ pub struct Page {

wayland_available: bool,
wayland_thread: Option<wayland::Sender>,
theme: Box<cosmic::cosmic_theme::Theme>,
high_contrast: Option<bool>,
}

#[derive(Debug, Clone)]
pub enum Message {
Event(wayland::AccessibilityEvent),
ProtocolUnavailable,
Return,
HighContrast(bool),
SystemTheme(Box<cosmic::cosmic_theme::Theme>),
}

impl page::Page<crate::pages::Message> for Page {
Expand Down Expand Up @@ -110,6 +118,7 @@ pub fn vision() -> section::Section<crate::pages::Message> {
on = fl!("accessibility", "on");
off = fl!("accessibility", "off");
unavailable = fl!("accessibility", "unavailable");
high_contrast = fl!("accessibility", "high-contrast");
});

Section::default()
Expand Down Expand Up @@ -154,6 +163,13 @@ pub fn vision() -> section::Section<crate::pages::Message> {
.then_some(crate::pages::Message::Page(magnifier_entity)),
)
})
.add(
cosmic::Element::from(
settings::item::builder(&descriptions[high_contrast])
.toggler(page.theme.is_high_contrast, Message::HighContrast),
)
.map(crate::pages::Message::Accessibility),
)
.into()
})
}
Expand All @@ -170,6 +186,65 @@ impl Page {
Message::Return => {
return cosmic::iced::Task::done(crate::app::Message::Page(self.entity))
}
Message::SystemTheme(theme) => {
self.theme = theme;
}
Message::HighContrast(enabled) => {
if self.theme.is_high_contrast == enabled
|| self.high_contrast.is_some_and(|hc| hc == enabled)
{
return Task::none();
}
self.high_contrast = Some(enabled);

_ = std::thread::spawn(move || {
let set_hc = |is_dark: bool| {
let builder_config = if is_dark {
ThemeBuilder::dark_config()?
} else {
ThemeBuilder::light_config()?
};
let mut builder = match ThemeBuilder::get_entry(&builder_config) {
Ok(b) => b,
Err((errs, b)) => {
tracing::warn!("{errs:?}");
b
}
};

builder.palette = if is_dark {
if enabled {
CosmicPalette::HighContrastDark(builder.palette.inner())
} else {
CosmicPalette::Dark(builder.palette.inner())
}
} else if enabled {
CosmicPalette::HighContrastLight(builder.palette.inner())
} else {
CosmicPalette::Light(builder.palette.inner())
};
builder.write_entry(&builder_config)?;

let new_theme = builder.build();

let theme_config = if is_dark {
CosmicTheme::dark_config()?
} else {
CosmicTheme::light_config()?
};

new_theme.write_entry(&theme_config)?;

Result::<(), cosmic_config::Error>::Ok(())
};
if let Err(err) = set_hc(true) {
tracing::warn!("{err:?}");
}
if let Err(err) = set_hc(false) {
tracing::warn!("{err:?}");
}
});
}
}

cosmic::iced::Task::none()
Expand Down
1 change: 1 addition & 0 deletions i18n/en/cosmic_settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ accessibility = Accessibility
.on = On
.off = Off
.unavailable = Unavailable
.high-contrast = High contrast mode
magnifier = Magnifier
.controls = Or use these shortcuts: { $zoom_in ->
[zero] {""}
Expand Down