Start implementing battery applet
This commit is contained in:
parent
ff2d7e0237
commit
198fe3dd71
4 changed files with 1195 additions and 0 deletions
|
|
@ -11,3 +11,7 @@ members = [
|
|||
"applets/cosmic-app-list",
|
||||
"applets/cosmic-panel-button",
|
||||
]
|
||||
|
||||
exclude = [
|
||||
"applets/cosmic-applet-power-battery",
|
||||
]
|
||||
|
|
|
|||
1007
applets/cosmic-applet-power-battery/Cargo.lock
generated
Normal file
1007
applets/cosmic-applet-power-battery/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
8
applets/cosmic-applet-power-battery/Cargo.toml
Normal file
8
applets/cosmic-applet-power-battery/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "cosmic-applet-power-battery"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
relm4 = { git = "https://github.com/relm4/relm4", branch = "next", features = ["macros"] }
|
||||
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
|
||||
176
applets/cosmic-applet-power-battery/src/main.rs
Normal file
176
applets/cosmic-applet-power-battery/src/main.rs
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
use gtk4::prelude::*;
|
||||
use relm4::{ComponentParts, ComponentSender, RelmApp, SimpleComponent, WidgetPlus};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Default)]
|
||||
struct AppModel {
|
||||
icon_name: String,
|
||||
battery_percent: u8,
|
||||
time_remaining: Duration,
|
||||
display_brightness: f64,
|
||||
keyboard_brightness: f64,
|
||||
}
|
||||
|
||||
enum AppMsg {
|
||||
SetDisplayBrightness(f64),
|
||||
SetKeyboardBrightness(f64),
|
||||
}
|
||||
|
||||
#[relm4::component]
|
||||
impl SimpleComponent for AppModel {
|
||||
type Widgets = AppWidgets;
|
||||
|
||||
type InitParams = ();
|
||||
|
||||
type Input = AppMsg;
|
||||
type Output = ();
|
||||
|
||||
view! {
|
||||
gtk4::Window {
|
||||
gtk4::MenuButton {
|
||||
set_has_frame: false,
|
||||
#[watch]
|
||||
set_icon_name: &model.icon_name,
|
||||
#[wrap(Some)]
|
||||
set_popover = >k4::Popover {
|
||||
#[wrap(Some)]
|
||||
set_child = >k4::Box {
|
||||
set_orientation: gtk4::Orientation::Vertical,
|
||||
|
||||
// Battery
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Image {
|
||||
#[watch]
|
||||
set_icon_name: Some(&model.icon_name),
|
||||
},
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Vertical,
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
set_label: "Battery",
|
||||
},
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
// XXX duration formatting
|
||||
#[watch]
|
||||
set_label: &format!("{:?} until empty ({}%)", model.time_remaining, model.battery_percent),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
// Profiles
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
// Limit charging
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Vertical,
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
set_label: "Limit Battery Charging",
|
||||
},
|
||||
gtk4::Label {
|
||||
set_halign: gtk4::Align::Start,
|
||||
set_label: "Increase the lifespan of your battery by setting a maximum charge value of 80%."
|
||||
},
|
||||
},
|
||||
gtk4::Switch {
|
||||
set_valign: gtk4::Align::Center,
|
||||
},
|
||||
},
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
// Brightness
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Image {
|
||||
set_icon_name: Some("display-brightness-symbolic"),
|
||||
},
|
||||
gtk4::Scale {
|
||||
set_hexpand: true,
|
||||
set_adjustment: >k4::Adjustment::new(0., 0., 100., 1., 1., 0.),
|
||||
#[watch]
|
||||
set_value: model.display_brightness,
|
||||
connect_change_value[sender] => move |_, _, value| {
|
||||
sender.input(AppMsg::SetDisplayBrightness(value));
|
||||
gtk4::Inhibit(false)
|
||||
},
|
||||
},
|
||||
gtk4::Label {
|
||||
#[watch]
|
||||
set_label: &format!("{:.0}%", model.display_brightness),
|
||||
},
|
||||
},
|
||||
gtk4::Box {
|
||||
set_orientation: gtk4::Orientation::Horizontal,
|
||||
gtk4::Image {
|
||||
set_icon_name: Some("keyboard-brightness-symbolic"),
|
||||
},
|
||||
gtk4::Scale {
|
||||
set_hexpand: true,
|
||||
set_adjustment: >k4::Adjustment::new(0., 0., 100., 1., 1., 0.),
|
||||
#[watch]
|
||||
set_value: model.keyboard_brightness,
|
||||
connect_change_value[sender] => move |_, _, value| {
|
||||
sender.input(AppMsg::SetKeyboardBrightness(value));
|
||||
gtk4::Inhibit(false)
|
||||
},
|
||||
},
|
||||
gtk4::Label {
|
||||
#[watch]
|
||||
set_label: &format!("{:.0}%", model.keyboard_brightness),
|
||||
},
|
||||
},
|
||||
|
||||
gtk4::Separator {
|
||||
},
|
||||
|
||||
gtk4::Button {
|
||||
set_label: "Power Settings...",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn init(
|
||||
_params: Self::InitParams,
|
||||
root: &Self::Root,
|
||||
sender: &ComponentSender<Self>,
|
||||
) -> ComponentParts<Self> {
|
||||
let model = AppModel {
|
||||
icon_name: "battery-symbolic".to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let widgets = view_output!();
|
||||
|
||||
ComponentParts { model, widgets }
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Self::Input, _sender: &ComponentSender<Self>) {
|
||||
match msg {
|
||||
AppMsg::SetDisplayBrightness(value) => {
|
||||
self.display_brightness = value;
|
||||
}
|
||||
AppMsg::SetKeyboardBrightness(value) => {
|
||||
self.keyboard_brightness = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app: RelmApp<AppModel> = RelmApp::new("relm4.test.simple");
|
||||
app.run(());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue