Separate CosmicAppletWindow and CosmicAppletButton

This commit is contained in:
Ian Douglas Scott 2022-07-05 14:41:09 -07:00
parent 74f6c2eca6
commit aac43de65d
16 changed files with 421 additions and 343 deletions

View file

@ -5,8 +5,8 @@ use crate::dock_list::DockList;
use crate::dock_list::DockListType;
use crate::utils::Event;
use cascade::cascade;
use cosmic_panel_config::config::PanelAnchor;
use cosmic_panel_config::config::CosmicPanelConfig;
use cosmic_panel_config::config::PanelAnchor;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use gtk4::Orientation;
@ -68,13 +68,12 @@ impl AppsContainer {
// Setup
self_.setup_callbacks();
self_.set_position(config.anchor);
Self::setup_callbacks(&self_);
self_
}
pub fn model(&self, type_: DockListType) -> &gio::ListStore {
// Get state
let imp = imp::AppsContainer::from_instance(self);

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0-only
use cosmic_panel_config::config::{PanelAnchor, CosmicPanelConfig};
use cosmic_panel_config::config::{CosmicPanelConfig, PanelAnchor};
use glib::SignalHandlerId;
use gtk4::subclass::prelude::*;
use gtk4::{gio, glib};
@ -25,7 +25,7 @@ pub struct DockList {
pub popover_menu_index: Rc<Cell<Option<u32>>>,
pub position: Rc<Cell<PanelAnchor>>,
pub tx: OnceCell<mpsc::Sender<Event>>,
pub config: OnceCell<CosmicPanelConfig>
pub config: OnceCell<CosmicPanelConfig>,
}
#[glib::object_subclass]

View file

@ -76,90 +76,93 @@ fn app(application: &Application) {
});
pa.connect().unwrap(); // XXX unwrap
view! {
window = libcosmic_applet::Applet {
window = libcosmic_applet::AppletWindow {
set_application: Some(application),
set_title: Some("COSMIC Network Applet"),
// TODO: adjust based on volume, mute
set_button_icon_name: "multimedia-volume-control-symbolic",
#[wrap(Some)]
set_popover_child: window_box = &GtkBox {
set_orientation: Orientation::Vertical,
set_spacing: 24,
append: output_box = &GtkBox {
set_orientation: Orientation::Horizontal,
set_spacing: 16,
append: output_icon = &Image {
set_icon_name: Some("audio-speakers-symbolic"),
},
append: output_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
set_format_value_func: |_, value| {
format!("{:.0}%", value)
},
set_value_pos: PositionType::Right,
set_hexpand: true
}
},
append: input_box = &GtkBox {
set_orientation: Orientation::Horizontal,
set_spacing: 16,
append: input_icon = &Image {
set_icon_name: Some("audio-input-microphone-symbolic"),
},
append: input_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
set_format_value_func: |_, value| {
format!("{:.0}%", value)
},
set_value_pos: PositionType::Right,
set_hexpand: true
}
},
append: _sep = &Separator {
set_orientation: Orientation::Horizontal,
},
append: output_list_box = &GtkBox {
set_child = &libcosmic_applet::AppletButton {
// TODO: adjust based on volume, mute
set_button_icon_name: "multimedia-volume-control-symbolic",
#[wrap(Some)]
set_popover_child: window_box = &GtkBox {
set_orientation: Orientation::Vertical,
append: current_output_button = &Button {
#[wrap(Some)]
set_child: current_output = &Label {},
connect_clicked[outputs_revealer] => move |_| {
outputs_revealer.set_reveal_child(!outputs_revealer.reveals_child());
set_spacing: 24,
append: output_box = &GtkBox {
set_orientation: Orientation::Horizontal,
set_spacing: 16,
append: output_icon = &Image {
set_icon_name: Some("audio-speakers-symbolic"),
},
append: output_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
set_format_value_func: |_, value| {
format!("{:.0}%", value)
},
set_value_pos: PositionType::Right,
set_hexpand: true
}
},
append: outputs_revealer = &Revealer {
set_transition_type: RevealerTransitionType::SlideDown,
#[wrap(Some)]
set_child: outputs = &ListBox {
set_selection_mode: SelectionMode::None,
set_activate_on_single_click: true
}
}
},
append: _sep = &Separator {
set_orientation: Orientation::Horizontal,
},
append: input_list_box = &GtkBox {
set_orientation: Orientation::Vertical,
append: current_input_button = &Button {
#[wrap(Some)]
set_child: current_input = &Label {},
connect_clicked[inputs_revealer] => move |_| {
inputs_revealer.set_reveal_child(!inputs_revealer.reveals_child());
append: input_box = &GtkBox {
set_orientation: Orientation::Horizontal,
set_spacing: 16,
append: input_icon = &Image {
set_icon_name: Some("audio-input-microphone-symbolic"),
},
append: input_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
set_format_value_func: |_, value| {
format!("{:.0}%", value)
},
set_value_pos: PositionType::Right,
set_hexpand: true
}
},
append: inputs_revealer = &Revealer {
set_transition_type: RevealerTransitionType::SlideDown,
#[wrap(Some)]
set_child: inputs = &ListBox {
set_selection_mode: SelectionMode::None,
set_activate_on_single_click: true
append: _sep = &Separator {
set_orientation: Orientation::Horizontal,
},
append: output_list_box = &GtkBox {
set_orientation: Orientation::Vertical,
append: current_output_button = &Button {
#[wrap(Some)]
set_child: current_output = &Label {},
connect_clicked[outputs_revealer] => move |_| {
outputs_revealer.set_reveal_child(!outputs_revealer.reveals_child());
}
},
append: outputs_revealer = &Revealer {
set_transition_type: RevealerTransitionType::SlideDown,
#[wrap(Some)]
set_child: outputs = &ListBox {
set_selection_mode: SelectionMode::None,
set_activate_on_single_click: true
}
}
},
append: _sep = &Separator {
set_orientation: Orientation::Horizontal,
},
append: input_list_box = &GtkBox {
set_orientation: Orientation::Vertical,
append: current_input_button = &Button {
#[wrap(Some)]
set_child: current_input = &Label {},
connect_clicked[inputs_revealer] => move |_| {
inputs_revealer.set_reveal_child(!inputs_revealer.reveals_child());
}
},
append: inputs_revealer = &Revealer {
set_transition_type: RevealerTransitionType::SlideDown,
#[wrap(Some)]
set_child: inputs = &ListBox {
set_selection_mode: SelectionMode::None,
set_activate_on_single_click: true
}
}
},
append: _sep = &Separator {
set_orientation: Orientation::Horizontal,
},
append: playing_apps = &ListBox {
set_selection_mode: SelectionMode::None,
}
},
append: _sep = &Separator {
set_orientation: Orientation::Horizontal,
},
append: playing_apps = &ListBox {
set_selection_mode: SelectionMode::None,
}
}
}

View file

@ -77,115 +77,118 @@ impl SimpleComponent for AppModel {
type Output = ();
view! {
libcosmic_applet::Applet {
#[watch]
set_button_icon_name: &model.icon_name,
libcosmic_applet::AppletWindow {
#[wrap(Some)]
set_popover_child = &gtk4::Box {
set_orientation: gtk4::Orientation::Vertical,
set_child = &libcosmic_applet::AppletButton {
#[watch]
set_button_icon_name: &model.icon_name,
#[wrap(Some)]
set_popover_child = &gtk4::Box {
set_orientation: gtk4::Orientation::Vertical,
// Battery
gtk4::Box {
set_orientation: gtk4::Orientation::Horizontal,
gtk4::Image {
#[watch]
set_icon_name: Some(&model.icon_name),
},
// Battery
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 time to full, fully changed, etc.
set_orientation: gtk4::Orientation::Horizontal,
gtk4::Image {
#[watch]
set_label: &format!("{} until empty ({:.0}%)", format_duration(model.time_remaining), model.battery_percent),
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 time to full, fully changed, etc.
#[watch]
set_label: &format!("{} until empty ({:.0}%)", format_duration(model.time_remaining), model.battery_percent),
},
},
},
},
gtk4::Separator {
},
gtk4::Separator {
},
// Limit charging
gtk4::Box {
set_orientation: gtk4::Orientation::Horizontal,
// Limit charging
gtk4::Box {
set_orientation: gtk4::Orientation::Vertical,
gtk4::Label {
set_halign: gtk4::Align::Start,
set_label: "Limit Battery Charging",
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 {
#[watch]
set_visible: model.backlight.is_some(),
set_orientation: gtk4::Orientation::Horizontal,
gtk4::Image {
set_icon_name: Some("display-brightness-symbolic"),
},
gtk4::Scale {
set_hexpand: true,
set_adjustment: &gtk4::Adjustment::new(0., 0., 1., 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 {
set_halign: gtk4::Align::Start,
set_label: "Increase the lifespan of your battery by setting a maximum charge value of 80%."
#[watch]
set_label: &format!("{:.0}%", model.display_brightness * 100.),
},
},
gtk4::Switch {
set_valign: gtk4::Align::Center,
},
},
gtk4::Separator {
},
// Brightness
gtk4::Box {
#[watch]
set_visible: model.backlight.is_some(),
set_orientation: gtk4::Orientation::Horizontal,
gtk4::Image {
set_icon_name: Some("display-brightness-symbolic"),
},
gtk4::Scale {
set_hexpand: true,
set_adjustment: &gtk4::Adjustment::new(0., 0., 1., 1., 1., 0.),
gtk4::Box {
#[watch]
set_value: model.display_brightness,
connect_change_value[sender] => move |_, _, value| {
sender.input(AppMsg::SetDisplayBrightness(value));
gtk4::Inhibit(false)
set_visible: model.kbd_backlight.is_some(),
set_orientation: gtk4::Orientation::Horizontal,
gtk4::Image {
set_icon_name: Some("keyboard-brightness-symbolic"),
},
gtk4::Scale {
set_hexpand: true,
set_adjustment: &gtk4::Adjustment::new(0., 0., 1., 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 * 100.),
},
},
gtk4::Label {
#[watch]
set_label: &format!("{:.0}%", model.display_brightness * 100.),
},
},
gtk4::Box {
#[watch]
set_visible: model.kbd_backlight.is_some(),
set_orientation: gtk4::Orientation::Horizontal,
gtk4::Image {
set_icon_name: Some("keyboard-brightness-symbolic"),
},
gtk4::Scale {
set_hexpand: true,
set_adjustment: &gtk4::Adjustment::new(0., 0., 1., 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 * 100.),
},
},
gtk4::Separator {
},
gtk4::Separator {
},
gtk4::Button {
set_label: "Power Settings...",
connect_clicked => move |_| {
// XXX open subpanel
let _ = Command::new("cosmic-settings").spawn();
// TODO hide
gtk4::Button {
set_label: "Power Settings...",
connect_clicked => move |_| {
// XXX open subpanel
let _ = Command::new("cosmic-settings").spawn();
// TODO hide
}
}
}
}

View file

@ -10,6 +10,7 @@ pub mod graphics;
pub mod mode_box;
use self::{dbus::PowerDaemonProxy, graphics::Graphics, mode_box::ModeSelection};
use cosmic_panel_config::config::CosmicPanelConfig;
use gtk4::{
gdk::Display,
gio::ApplicationFlags,
@ -20,7 +21,6 @@ use gtk4::{
};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
use cosmic_panel_config::config::CosmicPanelConfig;
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
@ -88,7 +88,7 @@ fn build_ui(application: &gtk4::Application) {
image.add_css_class("panel_icon");
image.set_pixel_size(config.get_applet_icon_size().try_into().unwrap());
button.set_child(Some(&image));
let current_graphics = RT
let current_graphics = RT
.block_on(get_current_graphics())
.expect("failed to connect to system76-power");
view! {

View file

@ -10,6 +10,7 @@ futures = "0.3"
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
libcosmic-applet = { path = "../../libcosmic-applet" }
once_cell = "1.12"
relm4-macros = { git = "https://github.com/Relm4/Relm4.git", branch = "next" }
serde = "1"
zbus = "2.0.1"
zbus_names = "2"

View file

@ -1,5 +1,5 @@
use cascade::cascade;
use gtk4::{glib, prelude::*};
use relm4_macros::view;
mod dbus_service;
mod deref_cell;
@ -19,18 +19,20 @@ fn main() {
let notification_list = NotificationList::new(&notifications);
let window = cascade! {
libcosmic_applet::Applet::new();
..set_button_icon_name("user-invisible-symbolic"); // TODO
..set_popover_child(Some(&notification_list));
..show();
};
view! {
window = libcosmic_applet::AppletWindow {
#[wrap(Some)]
set_child: applet_button = &libcosmic_applet::AppletButton {
set_button_icon_name: "user-invisible-symbolic", // TODO
set_popover_child: Some(&notification_list)
}
}
}
window.show();
// XXX show in correct place
cascade! {
NotificationPopover::new(&notifications);
..set_parent(&window.child().unwrap()); // XXX better way?
};
let notification_popover = NotificationPopover::new(&notifications);
notification_popover.set_parent(&applet_button);
let main_loop = glib::MainLoop::new(None, false);
main_loop.run();

View file

@ -24,34 +24,37 @@ fn main() {
fn build_ui(application: &gtk4::Application) {
view! {
window = libcosmic_applet::Applet {
window = libcosmic_applet::AppletWindow {
set_title: Some("COSMIC Power Applet"),
set_application: Some(application),
// TODO adjust battery icon based on charge
set_button_icon_name: "system-shutdown-symbolic",
#[wrap(Some)]
set_popover_child: main_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
set_margin_end: 24,
append: settings_button = &Button {
#[wrap(Some)]
set_child = &Label {
set_label: "Settings...",
set_halign: Align::Start,
set_hexpand: true
set_child = &libcosmic_applet::AppletButton {
set_button_icon_name: "system-shutdown-symbolic",
#[wrap(Some)]
set_popover_child: main_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
set_margin_end: 24,
append: settings_button = &Button {
#[wrap(Some)]
set_child = &Label {
set_label: "Settings...",
set_halign: Align::Start,
set_hexpand: true
},
connect_clicked => move |_| {
let _ = Command::new("cosmic-settings").spawn();
}
},
connect_clicked => move |_| {
let _ = Command::new("cosmic-settings").spawn();
}
},
append = &Separator {},
append: &ui::session::build(),
append: second_separator = &Separator {},
append: &ui::system::build(),
append = &Separator {},
append: &ui::session::build(),
append: second_separator = &Separator {},
append: &ui::system::build(),
}
}
}
}

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: MPL-2.0-only
use calloop::channel::SyncSender;
use gtk4::{
gdk::Display,
gio::{self, ApplicationFlags},
@ -13,7 +14,6 @@ use tokio::sync::mpsc;
use utils::{Activate, WorkspaceEvent};
use wayland::State;
use window::CosmicWorkspacesWindow;
use calloop::channel::SyncSender;
mod localize;
mod utils;

View file

@ -92,8 +92,8 @@ pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
running: true,
};
let loop_handle = event_loop.handle();
loop_handle.insert_source(workspaces_rx, |e, _, state| {
match e {
loop_handle
.insert_source(workspaces_rx, |e, _, state| match e {
Event::Msg(WorkspaceEvent::Activate(id)) => {
if let Some(w) = state
.workspace_groups
@ -136,15 +136,16 @@ pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
}
}
}
Event::Closed => if let Some(workspace_manager) = &mut state.workspace_manager {
for g in &mut state.workspace_groups {
g.workspace_group_handle.destroy();
Event::Closed => {
if let Some(workspace_manager) = &mut state.workspace_manager {
for g in &mut state.workspace_groups {
g.workspace_group_handle.destroy();
}
workspace_manager.stop();
}
workspace_manager.stop();
},
}
}).unwrap();
}
})
.unwrap();
while state.running {
event_loop
.dispatch(Duration::from_millis(16), &mut state)

View file

@ -43,9 +43,7 @@ impl WorkspaceButton {
new_button.connect_clicked(move |_| {
let id_clone = id.clone();
if !is_active {
let _ = TX.get()
.unwrap()
.send(WorkspaceEvent::Activate(id_clone));
let _ = TX.get().unwrap().send(WorkspaceEvent::Activate(id_clone));
}
});

View file

@ -61,9 +61,7 @@ impl WorkspaceList {
.build();
scroll_controller.connect_scroll(|_, dx, dy| {
let _ = TX.get()
.unwrap()
.send(WorkspaceEvent::Scroll(dx + dy));
let _ = TX.get().unwrap().send(WorkspaceEvent::Scroll(dx + dy));
Inhibit::default()
});