chore: add cosmic app list and cosmic panel buttons

This commit is contained in:
Ashley Wulber 2022-06-06 11:52:45 -04:00
parent 853caa5b84
commit 464338585c
No known key found for this signature in database
GPG key ID: BCD0B777C3F6E2FD
54 changed files with 3855 additions and 187 deletions

View file

@ -0,0 +1,28 @@
// SPDX-License-Identifier: MPL-2.0-only
use gtk4::{glib, subclass::prelude::*};
// Object holding the state
#[derive(Default)]
pub struct CosmicPanelAppButtonWindow {}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for CosmicPanelAppButtonWindow {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "CosmicPanelAppButtonWindow";
type Type = super::CosmicPanelAppButtonWindow;
type ParentType = gtk4::ApplicationWindow;
}
// Trait shared by all GObjects
impl ObjectImpl for CosmicPanelAppButtonWindow {}
// Trait shared by all widgets
impl WidgetImpl for CosmicPanelAppButtonWindow {}
// Trait shared by all windows
impl WindowImpl for CosmicPanelAppButtonWindow {}
// Trait shared by all application
impl ApplicationWindowImpl for CosmicPanelAppButtonWindow {}

View file

@ -0,0 +1,71 @@
// SPDX-License-Identifier: MPL-2.0-only
use crate::fl;
use cascade::cascade;
use cosmic_panel_config::config::CosmicPanelConfig;
use gtk4::{
gio::{self, DesktopAppInfo, Icon},
glib::{self, Object},
prelude::*,
Align, Application, Button, Orientation,
};
use std::process::Command;
mod imp;
glib::wrapper! {
pub struct CosmicPanelAppButtonWindow(ObjectSubclass<imp::CosmicPanelAppButtonWindow>)
@extends gtk4::ApplicationWindow, gtk4::Window, gtk4::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk4::Accessible, gtk4::Buildable,
gtk4::ConstraintTarget, gtk4::Native, gtk4::Root, gtk4::ShortcutManager;
}
impl CosmicPanelAppButtonWindow {
pub fn new(app: &Application, app_desktop_file_name: &str) -> Self {
let self_: Self = Object::new(&[("application", app)])
.expect("Failed to create `CosmicPanelButtonWindow`.");
cascade! {
&self_;
..set_width_request(1);
..set_height_request(1);
..set_decorated(false);
..set_resizable(false);
..set_title(Some(app_desktop_file_name));
..add_css_class("root_window");
};
if let Some(apps_desktop_info) = DesktopAppInfo::new(&format!("{}.desktop", app_desktop_file_name)) {
let app_button = cascade! {
Button::new();
..add_css_class("apps");
};
let config = CosmicPanelConfig::load_from_env().unwrap_or_default();
let icon = apps_desktop_info.icon().unwrap_or_else(|| {
Icon::for_string("image-missing").expect("Failed to set default icon")
});
let container = gtk4::Box::new(Orientation::Horizontal, 0);
let image = cascade! {
gtk4::Image::from_gicon(&icon);
..set_hexpand(true);
..set_halign(Align::Center);
..set_pixel_size(config.get_applet_icon_size().try_into().unwrap());
..set_tooltip_text(Some(&apps_desktop_info.name()));
};
container.append(&image);
app_button.set_child(Some(&container));
dbg!(apps_desktop_info.string("Exec").unwrap().as_str());
app_button.connect_clicked(move |_| {
let _ = Command::new("xdg-shell-wrapper")
.env_remove("WAYLAND_SOCKET")
.arg(apps_desktop_info.string("Exec").unwrap().as_str())
.spawn();
});
self_.set_child(Some(&app_button));
} else {
panic!("Requested application is not installed");
}
self_
}
}