Skip to content

Commit

Permalink
update iced
Browse files Browse the repository at this point in the history
  • Loading branch information
git-f0x authored and ids1024 committed Nov 19, 2024
1 parent c6fda40 commit 7eedda7
Show file tree
Hide file tree
Showing 9 changed files with 2,097 additions and 1,008 deletions.
2,795 changes: 1,935 additions & 860 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ i18n-embed = { version = "0.14", features = [
] }
i18n-embed-fl = "0.8"
libcosmic = { git = "https://github.com/pop-os/libcosmic", default-features = false, features = [
"autosize",
"tokio",
"wayland",
"multi-window",
"winit",
] }
libpulse-binding = "2.28.1"
log = "0.4.21"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prefix ?= /usr/local
prefix ?= /usr
polkit-agent-helper-1 ?= /usr/libexec/polkit-agent-helper-1
bindir = $(prefix)/bin
libdir = $(prefix)/lib
Expand Down
46 changes: 23 additions & 23 deletions src/components/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![allow(irrefutable_let_patterns)]

use cosmic::{
iced::{self, Command, Subscription},
iced_runtime::window::Id as SurfaceId,
};
use cosmic::iced::{self, window::Id as SurfaceId, Subscription, Task};
use std::{
collections::HashMap,
time::{Duration, Instant},
Expand Down Expand Up @@ -53,7 +50,7 @@ struct App {
}

impl App {
fn create_indicator(&mut self, params: osd_indicator::Params) -> cosmic::app::Command<Msg> {
fn create_indicator(&mut self, params: osd_indicator::Params) -> cosmic::app::Task<Msg> {
if let Some((_id, ref mut state)) = &mut self.indicator {
state.replace_params(params)
} else {
Expand All @@ -72,7 +69,7 @@ impl cosmic::Application for App {
type Flags = ();
const APP_ID: &'static str = "com.system76.CosmicWorkspaces";

fn init(core: cosmic::app::Core, _flags: ()) -> (Self, cosmic::app::Command<Msg>) {
fn init(core: cosmic::app::Core, _flags: ()) -> (Self, cosmic::app::Task<Msg>) {
(
Self {
core,
Expand All @@ -91,7 +88,7 @@ impl cosmic::Application for App {
source_volume: None,
airplane_mode: None,
},
Command::none(),
Task::none(),
)
}

Expand All @@ -103,7 +100,7 @@ impl cosmic::Application for App {
&mut self.core
}

fn update(&mut self, message: Msg) -> cosmic::app::Command<Msg> {
fn update(&mut self, message: Msg) -> cosmic::app::Task<Msg> {
match message {
Msg::DBus(event) => {
match event {
Expand All @@ -115,7 +112,7 @@ impl cosmic::Application for App {
log::error!("Failed to {}: {}", context, err);
}
}
iced::Command::none()
iced::Task::none()
}
Msg::PolkitAgent(event) => match event {
polkit_agent::Event::CreateDialog(params) => {
Expand All @@ -141,7 +138,7 @@ impl cosmic::Application for App {
unreachable!()
}
} else {
Command::none()
Task::none()
}
}
},
Expand All @@ -154,7 +151,7 @@ impl cosmic::Application for App {
return cmd
.map(move |msg| cosmic::app::Message::App(Msg::PolkitDialog((id, msg))));
}
Command::none()
Task::none()
}
Msg::OsdIndicator(msg) => {
if let Some((id, state)) = self.indicator.take() {
Expand All @@ -164,25 +161,25 @@ impl cosmic::Application for App {
}
cmd.map(|x| cosmic::app::Message::App(Msg::OsdIndicator(x)))
} else {
Command::none()
Task::none()
}
}
Msg::SettingsDaemon(settings_daemon::Event::Sender(_)) => Command::none(),
Msg::SettingsDaemon(settings_daemon::Event::Sender(_)) => Task::none(),
Msg::SettingsDaemon(settings_daemon::Event::MaxDisplayBrightness(max_brightness)) => {
self.max_display_brightness = Some(max_brightness);
Command::none()
Task::none()
}
Msg::SettingsDaemon(settings_daemon::Event::DisplayBrightness(brightness)) => {
if self.display_brightness.is_none() {
self.display_brightness = Some(brightness);
Command::none()
Task::none()
} else if self.display_brightness != Some(brightness) {
self.display_brightness = Some(brightness);
self.create_indicator(osd_indicator::Params::DisplayBrightness(
brightness as f64 / self.max_display_brightness.unwrap_or(100) as f64,
))
} else {
Command::none()
Task::none()
}
}
Msg::Pulse(evt) => {
Expand Down Expand Up @@ -244,8 +241,11 @@ impl cosmic::Application for App {
}
}
}
pulse::Event::CardInfo(_) => {}
pulse::Event::DefaultSink(_) => {}
pulse::Event::DefaultSource(_) => {}
}
Command::none()
Task::none()
}
Msg::AirplaneMode(state) => {
if self.airplane_mode.is_none() {
Expand All @@ -254,29 +254,29 @@ impl cosmic::Application for App {
self.airplane_mode = Some(state);
return self.create_indicator(osd_indicator::Params::AirplaneMode(state));
}
Command::none()
Task::none()
}
Msg::KeyboardBacklight(update) => match update {
KeyboardBacklightUpdate::Sender(_) => Command::none(),
KeyboardBacklightUpdate::Sender(_) => Task::none(),
KeyboardBacklightUpdate::MaxBrightness(max_brightness) => {
self.max_keyboard_brightness = Some(max_brightness);
Command::none()
Task::none()
}
KeyboardBacklightUpdate::Brightness(brightness) => {
if self.keyboard_brightness.is_none() {
self.keyboard_brightness = Some(brightness);
Command::none()
Task::none()
} else if self.keyboard_brightness != Some(brightness) {
self.keyboard_brightness = Some(brightness);
if let Some(max_brightness) = self.max_keyboard_brightness {
self.create_indicator(osd_indicator::Params::KeyboardBrightness(
brightness as f64 / max_brightness as f64,
))
} else {
Command::none()
Task::none()
}
} else {
Command::none()
Task::none()
}
}
},
Expand Down
100 changes: 47 additions & 53 deletions src/components/osd_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@
// TODO: Dismiss on click?

use cosmic::{
iced::{
self,
wayland::{
actions::layer_surface::IcedMargin,
layer_surface::{Anchor, KeyboardInteractivity, Layer},
},
widget, Border, Command,
iced::{self, window::Id as SurfaceId, Alignment, Border, Length},
iced_runtime::platform_specific::wayland::layer_surface::{
IcedMargin, SctkLayerSurfaceSettings,
},
iced_runtime::{
command::platform_specific::wayland::layer_surface::SctkLayerSurfaceSettings,
window::Id as SurfaceId,
iced_winit::commands::layer_surface::{
destroy_layer_surface, get_layer_surface, Anchor, KeyboardInteractivity, Layer,
},
iced_sctk::commands::layer_surface::{destroy_layer_surface, get_layer_surface},
Element,
widget, Element, Task,
};
use futures::future::{abortable, AbortHandle, Aborted};

use once_cell::sync::Lazy;
use std::time::Duration;

pub static OSD_INDICATOR_ID: Lazy<widget::Id> =
Lazy::new(|| widget::Id::new("osd-indicator".to_string()));

#[derive(Debug)]
pub enum Params {
DisplayBrightness(f64),
Expand Down Expand Up @@ -90,12 +87,12 @@ pub struct State {
timer_abort: AbortHandle,
}

fn close_timer() -> (Command<Msg>, AbortHandle) {
fn close_timer() -> (Task<Msg>, AbortHandle) {
let (future, timer_abort) = abortable(async {
let duration = Duration::from_secs(3);
tokio::time::sleep(duration).await;
});
let command = Command::perform(future, |res| {
let command = Task::perform(future, |res| {
if res == Err(Aborted) {
Msg::Ignore
} else {
Expand All @@ -106,7 +103,7 @@ fn close_timer() -> (Command<Msg>, AbortHandle) {
}

impl State {
pub fn new(id: SurfaceId, params: Params) -> (Self, Command<Msg>) {
pub fn new(id: SurfaceId, params: Params) -> (Self, Task<Msg>) {
// Anchor to bottom right, with margin?
let mut cmds = vec![];
cmds.push(get_layer_surface(SctkLayerSurfaceSettings {
Expand All @@ -131,13 +128,13 @@ impl State {
params,
timer_abort,
},
Command::batch(cmds),
Task::batch(cmds),
)
}

// Re-use OSD surface to show a different OSD
// Resets close timer
pub fn replace_params(&mut self, params: Params) -> Command<Msg> {
pub fn replace_params(&mut self, params: Params) -> Task<Msg> {
self.params = params;
// Reset timer
self.timer_abort.abort();
Expand All @@ -147,47 +144,41 @@ impl State {
}

pub fn view(&self) -> Element<'_, Msg> {
let icon = cosmic::widget::icon::from_name(self.params.icon_name());
let icon = widget::icon::from_name(self.params.icon_name());

// Use large radius on value-OSD to enforce pill-shape with "Round" system style
let radius;

let osd_contents = if let Some(value) = self.params.value() {
radius = cosmic::theme::active().cosmic().radius_l();
const OSD_WIDTH: f32 = 392.0;
const OSD_HEIGHT: f32 = 52.0;
const FLANK_WIDTH: f32 = 36.0;
const SPACING: f32 = 12.0;
const BAR_WIDTH: f32 = OSD_WIDTH - 2.0 * FLANK_WIDTH - 1.15 * OSD_HEIGHT;
cosmic::widget::container(
widget::row![
cosmic::widget::container(icon.size(20))
.width(FLANK_WIDTH)
.align_x(iced::alignment::Horizontal::Center),
cosmic::widget::horizontal_space(SPACING),
cosmic::widget::progress_bar(0. ..=100., value as f32)
widget::container(
iced::widget::row![
widget::container(icon.size(20))
.width(Length::Fixed(32.0))
.align_x(Alignment::Center),
widget::text::body(format!("{}%", value))
.width(Length::Fixed(32.0))
.align_x(Alignment::Center),
widget::horizontal_space().width(Length::Fixed(8.0)),
widget::progress_bar(0. ..=100., value as f32)
.height(4)
.width(BAR_WIDTH),
cosmic::widget::text(format!("{}%", value))
.size(16)
.width(FLANK_WIDTH + SPACING)
.horizontal_alignment(iced::alignment::Horizontal::Right),
.width(Length::Fixed(266.0)),
]
.align_items(iced::Alignment::Center),
.align_y(Alignment::Center),
)
.width(OSD_WIDTH)
.height(OSD_HEIGHT)
.width(Length::Fixed(392.0))
.height(Length::Fixed(52.0))
} else {
radius = cosmic::theme::active().cosmic().radius_m();
const ICON_SIZE: u16 = 112;
cosmic::widget::container(icon.size(ICON_SIZE))
widget::container(icon.size(ICON_SIZE))
.width(ICON_SIZE + 2 * cosmic::theme::active().cosmic().space_l())
.height(ICON_SIZE + 2 * cosmic::theme::active().cosmic().space_s())
};

// Define overall style of OSD container
let container_style = cosmic::theme::Container::custom(move |theme| {
cosmic::iced_style::container::Appearance {
let container_style =
cosmic::theme::Container::custom(move |theme| widget::container::Style {
text_color: Some(theme.cosmic().on_bg_color().into()),
background: Some(iced::Color::from(theme.cosmic().bg_color()).into()),
border: Border {
Expand All @@ -197,21 +188,24 @@ impl State {
},
shadow: Default::default(),
icon_color: Some(theme.cosmic().on_bg_color().into()),
}
});

// Apply style and center contents
osd_contents
.style(container_style)
.align_x(iced::alignment::Horizontal::Center)
.align_y(iced::alignment::Vertical::Center)
.into()
});

widget::autosize::autosize(
osd_contents
.class(container_style)
.align_x(Alignment::Center)
.align_y(Alignment::Center),
OSD_INDICATOR_ID.clone(),
)
.min_width(1.)
.min_height(1.)
.into()
}

pub fn update(self, msg: Msg) -> (Option<Self>, Command<Msg>) {
pub fn update(self, msg: Msg) -> (Option<Self>, Task<Msg>) {
log::trace!("indicator msg: {:?}", msg);
match msg {
Msg::Ignore => (Some(self), Command::none()),
Msg::Ignore => (Some(self), Task::none()),
Msg::Close => (None, destroy_layer_surface(self.id)),
}
}
Expand Down
Loading

0 comments on commit 7eedda7

Please sign in to comment.