chore: remove graphics modes and night light

This commit is contained in:
Vukašin Vojinović 2024-05-13 22:20:40 +02:00 committed by GitHub
parent c4224ff36f
commit bed629563e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 122 additions and 678 deletions

View file

@ -1,245 +0,0 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use super::{Message, NightLight};
use crate::pages;
use cosmic::iced_core::{Alignment, Length, Padding};
use cosmic::prelude::CollectionWidget;
use cosmic::widget::{button, column, icon, list_column, row, toggler};
use cosmic::{Apply, Command, Element};
use std::sync::Arc;
pub const INTEGRATED: &str = "integrated";
pub const NVIDIA: &str = "nvidia";
pub const HYBRID: &str = "hybrid";
pub const COMPUTE: &str = "compute";
pub async fn fetch() -> Option<Arc<std::io::Result<Mode>>> {
let switchable = tokio::process::Command::new("system76-power")
.args(["graphics", "switchable"])
.output()
.await
.map(|output| {
std::str::from_utf8(&output.stdout).map_or(false, |text| text.trim() == "switchable")
});
match switchable {
Ok(false) => None,
Ok(true) => Some(Arc::new(
tokio::process::Command::new("system76-power")
.arg("graphics")
.output()
.await
.and_then(|output| {
if let Ok(mut mode) = std::str::from_utf8(&output.stdout) {
mode = mode.trim();
if mode == COMPUTE {
Ok(Mode::Compute)
} else if mode == HYBRID {
Ok(Mode::Hybrid)
} else if mode == INTEGRATED {
Ok(Mode::Integrated)
} else if mode == NVIDIA {
Ok(Mode::Nvidia)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"unknown graphics mode",
))
}
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"system76-power output was not UTF-8",
))
}
}),
)),
Err(why) => Some(Arc::new(Err(why))),
}
}
pub fn view(
mode: &'static str,
description: &'static str,
button: Option<(&'static str, Message)>,
) -> Element<'static, Message> {
let theme = cosmic::theme::active();
let theme = theme.cosmic();
let has_checkmark = button.is_none();
let content = column::with_capacity(3)
.padding(Padding::from([theme.space_xxs(), theme.space_l()]))
.push(cosmic::widget::text::body(mode))
.push(cosmic::widget::text::caption(description))
.push(cosmic::widget::Space::new(Length::Fill, 12))
.push_maybe(button.map(|(text, message)| {
button::text(text)
.style(cosmic::theme::Button::Link)
.trailing_icon(icon::from_name("go-next-symbolic").size(16))
.padding(0)
.on_press(message)
}));
if has_checkmark {
row::with_capacity(2)
.align_items(Alignment::Center)
.push(content)
.push(icon::from_name("object-select-symbolic").size(24))
.apply(Element::from)
.apply(cosmic::widget::list::container)
.into()
} else {
cosmic::widget::list::container(content).into()
}
}
/// Switchable graphics mode
#[derive(Clone, Copy, Debug)]
pub enum Mode {
Compute,
Hybrid,
Integrated,
Nvidia,
}
impl Mode {
#[must_use]
pub fn argument_str(self) -> &'static str {
match self {
Self::Compute => COMPUTE,
Self::Hybrid => HYBRID,
Self::Integrated => INTEGRATED,
Self::Nvidia => NVIDIA,
}
}
#[must_use]
pub fn localized_str(self) -> &'static str {
match self {
Self::Compute => &super::text::GRAPHICS_MODE_COMPUTE,
Self::Hybrid => &super::text::GRAPHICS_MODE_HYBRID,
Self::Integrated => &super::text::GRAPHICS_MODE_INTEGRATED,
Self::Nvidia => &super::text::GRAPHICS_MODE_NVIDIA,
}
}
}
impl super::Page {
pub fn graphics_mode_view(&self) -> Element<pages::Message> {
let mut container = list_column();
if let Some(graphics_mode) = self.config.graphics_mode {
// Displays the active graphics mode, and a button for configuring it.
container = container.add(cosmic::widget::settings::item(
&*super::text::GRAPHICS_MODE,
row()
.align_items(Alignment::Center)
.push(cosmic::widget::text::body(graphics_mode.localized_str()))
.push(
button::icon(icon::from_name("go-next-symbolic"))
.extra_small()
.on_press(Message::GraphicsModeContext),
),
));
}
// Displays the night light status, and a button for configuring it.
container = container.add(
cosmic::widget::settings::item::builder(&*super::text::NIGHT_LIGHT)
.description(&*super::text::NIGHT_LIGHT_DESCRIPTION)
.control(
row()
.align_items(Alignment::Center)
.push(toggler(None, self.config.night_light_enabled, |enable| {
Message::NightLight(NightLight::Toggle(enable))
}))
.push(
button::icon(icon::from_name("go-next-symbolic"))
.extra_small()
.on_press(Message::NightLightContext),
),
),
);
container
.apply(Element::from)
.map(crate::pages::Message::Displays)
}
pub fn graphics_mode_context_view(&self) -> Element<pages::Message> {
let theme = cosmic::theme::active();
let theme = theme.cosmic();
column::with_capacity(4)
.spacing(theme.space_xs())
.push(view(
&super::text::GRAPHICS_MODE_INTEGRATED,
&super::text::GRAPHICS_MODE_INTEGRATED_DESC,
if let Some(Mode::Integrated) = self.config.graphics_mode {
None
} else {
Some((
&super::text::GRAPHICS_MODE_INTEGRATED_ENABLE,
Message::GraphicsMode(Mode::Integrated),
))
},
))
.push(view(
&super::text::GRAPHICS_MODE_NVIDIA,
&super::text::GRAPHICS_MODE_NVIDIA_DESC,
if let Some(Mode::Nvidia) = self.config.graphics_mode {
None
} else {
Some((
&super::text::GRAPHICS_MODE_NVIDIA_ENABLE,
Message::GraphicsMode(Mode::Nvidia),
))
},
))
.push(view(
&super::text::GRAPHICS_MODE_HYBRID,
&super::text::GRAPHICS_MODE_HYBRID_DESC,
if let Some(Mode::Hybrid) = self.config.graphics_mode {
None
} else {
Some((
&super::text::GRAPHICS_MODE_HYBRID_ENABLE,
Message::GraphicsMode(Mode::Hybrid),
))
},
))
.push(view(
&super::text::GRAPHICS_MODE_COMPUTE,
&super::text::GRAPHICS_MODE_COMPUTE_DESC,
if let Some(Mode::Compute) = self.config.graphics_mode {
None
} else {
Some((
&super::text::GRAPHICS_MODE_COMPUTE_ENABLE,
Message::GraphicsMode(Mode::Compute),
))
},
))
.apply(Element::from)
.map(pages::Message::Displays)
}
/// Change the graphics mode.
pub fn set_graphics_mode(&mut self, mode: Mode) -> Command<crate::app::Message> {
self.config.graphics_mode = Some(mode);
// Runs `system76-power graphics {{mode}}`
cosmic::command::future(async move {
let result = tokio::process::Command::new("system76-power")
.args(["graphics", mode.argument_str()])
.status()
.await;
let page_message =
crate::pages::Message::Displays(Message::GraphicsModeResult(Arc::new(result)));
crate::app::Message::PageMessage(page_message)
})
}
}

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only
pub mod arrangement;
pub mod graphics;
// pub mod night_light;
pub mod text;
use crate::{app, pages};
@ -25,10 +25,9 @@ use std::{process::ExitStatus, sync::Arc};
pub struct ColorDepth(usize);
/// Identifies the content to display in the context drawer
pub enum ContextDrawer {
GraphicsMode,
NightLight,
}
// pub enum ContextDrawer {
// NightLight,
// }
/// Display mirroring options
#[derive(Clone, Copy, Debug)]
@ -40,17 +39,17 @@ pub enum Mirroring {
}
/// Night light preferences
#[derive(Clone, Copy, Debug)]
pub enum NightLight {
// #[derive(Clone, Copy, Debug)]
// pub enum NightLight {
/// Toggles night light's automatic scheduling.
AutoSchedule(bool),
// AutoSchedule(bool),
/// Sets the night light schedule.
ManualSchedule,
// ManualSchedule,
/// Changes the preferred night light temperature.
Temperature(f32),
// Temperature(f32),
/// Toggles night light mode
Toggle(bool),
}
// Toggle(bool),
// }
#[derive(Clone, Debug)]
pub enum Message {
@ -64,18 +63,12 @@ pub enum Message {
ColorProfile(usize),
/// Toggles display on or off.
DisplayToggle(bool),
/// Changes the hybrid graphics mode.
GraphicsMode(graphics::Mode),
/// Shows the graphics mode context drawer.
GraphicsModeContext,
/// Status of an applied graphics mode change
GraphicsModeResult(Arc<std::io::Result<ExitStatus>>),
/// Configures mirroring status of a display.
Mirroring(Mirroring),
/// Handle night light preferences.
NightLight(NightLight),
// NightLight(NightLight),
/// Show the night light mode context drawer.
NightLightContext,
// NightLightContext,
/// Set the orientation of a display.
Orientation(Transform),
/// Status of an applied display change.
@ -90,9 +83,6 @@ pub enum Message {
Scale(usize),
/// Refreshes display outputs.
Update {
/// The current graphics mode
graphics: Option<Arc<std::io::Result<graphics::Mode>>>,
/// Available outputs from cosmic-randr.
randr: Arc<Result<List, cosmic_randr_shell::Error>>,
},
@ -123,7 +113,7 @@ pub struct Page {
background_service: Option<tokio::task::JoinHandle<()>>,
config: Config,
cache: ViewCache,
context: Option<ContextDrawer>,
// context: Option<ContextDrawer>,
display_arrangement_scrollable: cosmic::widget::Id,
}
@ -136,7 +126,7 @@ impl Default for Page {
background_service: None,
config: Config::default(),
cache: ViewCache::default(),
context: None,
// context: None,
display_arrangement_scrollable: cosmic::widget::Id::unique(),
}
}
@ -145,8 +135,7 @@ impl Default for Page {
#[derive(Default)]
struct Config {
/// Whether night light is enabled.
night_light_enabled: bool,
graphics_mode: Option<graphics::Mode>,
// night_light_enabled: bool,
refresh_rate: Option<u32>,
resolution: Option<(u32, u32)>,
scale: u32,
@ -173,21 +162,16 @@ impl page::Page<crate::pages::Message> for Page {
sections: &mut SlotMap<section::Entity, Section<crate::pages::Message>>,
) -> Option<page::Content> {
Some(vec![
// Graphics switching and light mode
sections.insert(
Section::default()
.descriptions(vec![
text::GRAPHICS_MODE.as_str().into(),
text::GRAPHICS_MODE_COMPUTE_DESC.as_str().into(),
text::GRAPHICS_MODE_HYBRID_DESC.as_str().into(),
text::GRAPHICS_MODE_INTEGRATED_DESC.as_str().into(),
text::GRAPHICS_MODE_NVIDIA_DESC.as_str().into(),
text::NIGHT_LIGHT.as_str().into(),
text::NIGHT_LIGHT_AUTO.as_str().into(),
text::NIGHT_LIGHT_DESCRIPTION.as_str().into(),
])
.view::<Page>(|_binder, page, _section| page.graphics_mode_view()),
),
// Night light
// sections.insert(
// Section::default()
// .descriptions(vec![
// text::NIGHT_LIGHT.as_str().into(),
// text::NIGHT_LIGHT_AUTO.as_str().into(),
// text::NIGHT_LIGHT_DESCRIPTION.as_str().into(),
// ])
// .view::<Page>(|_binder, page, _section| page.night_light_view()),
// ),
// Display arrangement
sections.insert(
Section::default()
@ -309,21 +293,19 @@ impl page::Page<crate::pages::Message> for Page {
});
crate::pages::Message::Displays(Message::Update {
graphics: graphics::fetch().await,
randr: Arc::new(Ok(randr)),
})
})
}
// fn context_drawer(&self) -> Option<Element<pages::Message>> {
// Some(match self.context {
fn context_drawer(&self) -> Option<Element<pages::Message>> {
Some(match self.context {
Some(ContextDrawer::GraphicsMode) => self.graphics_mode_context_view(),
// Some(ContextDrawer::NightLight) => self.night_light_context_view(),
Some(ContextDrawer::NightLight) => self.night_light_context_view(),
None => return None,
})
}
// None => return None,
// })
// }
}
impl Page {
@ -343,36 +325,21 @@ impl Page {
Message::DisplayToggle(enable) => return self.toggle_display(enable),
Message::GraphicsMode(mode) => return self.set_graphics_mode(mode),
Message::GraphicsModeContext => {
self.context = Some(ContextDrawer::GraphicsMode);
return cosmic::command::message(app::Message::OpenContextDrawer(
text::GRAPHICS_MODE.clone().into(),
));
}
Message::GraphicsModeResult(result) => {
if let Some(Err(why)) = Arc::into_inner(result) {
tracing::error!(?why, "system76-power error");
}
}
Message::Mirroring(mirroring) => match mirroring {
Mirroring::Disable => (),
Mirroring::Mirror(target_display) => (),
Mirroring::Project(target_display) => (),
Mirroring::ProjectToAll => (),
},
Message::NightLight(night_light) => {}
Message::NightLightContext => {
self.context = Some(ContextDrawer::NightLight);
return cosmic::command::message(app::Message::OpenContextDrawer(
text::NIGHT_LIGHT.clone().into(),
));
}
// Message::NightLight(night_light) => {}
//
// Message::NightLightContext => {
// self.context = Some(ContextDrawer::NightLight);
// return cosmic::command::message(app::Message::OpenContextDrawer(
// text::NIGHT_LIGHT.clone().into(),
// ));
// }
Message::Orientation(orientation) => return self.set_orientation(orientation),
@ -390,19 +357,7 @@ impl Page {
Message::Scale(scale) => return self.set_scale(scale),
Message::Update { graphics, randr } => {
match graphics.and_then(Arc::into_inner) {
Some(Ok(mode)) => {
self.config.graphics_mode = Some(mode);
}
Some(Err(why)) => {
tracing::error!(?why, "error fetching graphics switching mode");
}
None => (),
}
Message::Update { randr } => {
match Arc::into_inner(randr) {
Some(Ok(outputs)) => self.update_displays(outputs),
@ -529,11 +484,11 @@ impl Page {
.apply(Element::from)
.map(pages::Message::Displays)
}
/// Displays the night light context drawer.
pub fn night_light_context_view(&self) -> Element<pages::Message> {
column().into()
}
// pub fn night_light_context_view(&self) -> Element<pages::Message> {
// column().into()
// }
/// Reloads the display list, and all information relevant to the active display.
pub fn update_displays(&mut self, list: List) {
@ -867,12 +822,10 @@ fn cache_rates(cached_rates: &mut Vec<String>, rates: &[u32]) {
}
pub async fn on_enter() -> crate::pages::Message {
let graphics_fut = graphics::fetch();
let randr_fut = cosmic_randr_shell::list();
let (graphics, randr) = futures::future::zip(graphics_fut, randr_fut).await;
let randr = futures::future::ready(randr_fut).await;
crate::pages::Message::Displays(Message::Update {
graphics,
randr: Arc::new(randr),
randr: Arc::new(randr.await),
})
}

View file

@ -0,0 +1,73 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use super::{Message, NightLight};
use crate::pages;
use cosmic::iced_core::{Alignment, Length, Padding};
use cosmic::prelude::CollectionWidget;
use cosmic::widget::{button, column, icon, list_column, row, toggler};
use cosmic::{Apply, Command, Element};
use std::sync::Arc;
pub fn view(
mode: &'static str,
description: &'static str,
button: Option<(&'static str, Message)>,
) -> Element<'static, Message> {
let theme = cosmic::theme::active();
let theme = theme.cosmic();
let has_checkmark = button.is_none();
let content = column::with_capacity(3)
.padding(Padding::from([theme.space_xxs(), theme.space_l()]))
.push(cosmic::widget::text::body(mode))
.push(cosmic::widget::text::caption(description))
.push(cosmic::widget::Space::new(Length::Fill, 12))
.push_maybe(button.map(|(text, message)| {
button::text(text)
.style(cosmic::theme::Button::Link)
.trailing_icon(icon::from_name("go-next-symbolic").size(16))
.padding(0)
.on_press(message)
}));
if has_checkmark {
row::with_capacity(2)
.align_items(Alignment::Center)
.push(content)
.push(icon::from_name("object-select-symbolic").size(24))
.apply(Element::from)
.apply(cosmic::widget::list::container)
.into()
} else {
cosmic::widget::list::container(content).into()
}
}
impl super::Page {
pub fn night_light_view(&self) -> Element<pages::Message> {
let mut container = list_column();
// Displays the night light status, and a button for configuring it.
container = container.add(
cosmic::widget::settings::item::builder(&*super::text::NIGHT_LIGHT)
.description(&*super::text::NIGHT_LIGHT_DESCRIPTION)
.control(
row()
.align_items(Alignment::Center)
.push(toggler(None, self.config.night_light_enabled, |enable| {
Message::NightLight(NightLight::Toggle(enable))
}))
.push(
button::icon(icon::from_name("go-next-symbolic"))
.extra_small()
.on_press(Message::NightLightContext),
),
),
);
container
.apply(Element::from)
.map(crate::pages::Message::Displays)
}
}

View file

@ -19,35 +19,6 @@ crate::cache_dynamic_lazy! {
pub static DISPLAY_RESOLUTION: String = fl!("display", "resolution");
pub static DISPLAY_SCALE: String = fl!("display", "scale");
pub static GRAPHICS_MODE: String = fl!("graphics-mode");
pub static GRAPHICS_MODE_COMPUTE: String =
fl!("graphics-mode", "mode", mode = super::graphics::COMPUTE);
pub static GRAPHICS_MODE_COMPUTE_ENABLE: String =
fl!("graphics-mode", "enable", mode = super::graphics::COMPUTE);
pub static GRAPHICS_MODE_COMPUTE_DESC: String =
fl!("graphics-mode", "desc", mode = super::graphics::COMPUTE);
pub static GRAPHICS_MODE_HYBRID: String =
fl!("graphics-mode", "mode", mode = super::graphics::HYBRID);
pub static GRAPHICS_MODE_HYBRID_ENABLE: String =
fl!("graphics-mode", "enable", mode = super::graphics::HYBRID);
pub static GRAPHICS_MODE_HYBRID_DESC: String =
fl!("graphics-mode", "desc", mode = super::graphics::HYBRID);
pub static GRAPHICS_MODE_INTEGRATED: String =
fl!("graphics-mode", "mode", mode = super::graphics::INTEGRATED);
pub static GRAPHICS_MODE_INTEGRATED_ENABLE: String = fl!(
"graphics-mode",
"enable",
mode = super::graphics::INTEGRATED
);
pub static GRAPHICS_MODE_INTEGRATED_DESC: String =
fl!("graphics-mode", "desc", mode = super::graphics::INTEGRATED);
pub static GRAPHICS_MODE_NVIDIA: String =
fl!("graphics-mode", "mode", mode = super::graphics::NVIDIA);
pub static GRAPHICS_MODE_NVIDIA_ENABLE: String =
fl!("graphics-mode", "enable", mode = super::graphics::NVIDIA);
pub static GRAPHICS_MODE_NVIDIA_DESC: String =
fl!("graphics-mode", "desc", mode = super::graphics::NVIDIA);
pub static MIRRORING: String = fl!("mirroring");
pub static NIGHT_LIGHT: String = fl!("night-light");