very simple alignment of dock on bottom edge
This commit is contained in:
parent
fc8a51389f
commit
09bdb53f8e
12 changed files with 959 additions and 1 deletions
54
examples/dock/window/imp.rs
Normal file
54
examples/dock/window/imp.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use gtk4 as gtk;
|
||||
|
||||
use glib::subclass::InitializingObject;
|
||||
use gtk::prelude::*;
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{gio, glib};
|
||||
use gtk::{CompositeTemplate, Entry, ListView};
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
// Object holding the state
|
||||
#[derive(CompositeTemplate, Default)]
|
||||
#[template(file = "window.ui")]
|
||||
pub struct Window {
|
||||
#[template_child]
|
||||
pub list_view: TemplateChild<ListView>,
|
||||
pub model: OnceCell<gio::ListStore>,
|
||||
}
|
||||
|
||||
// The central trait for subclassing a GObject
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for Window {
|
||||
// `NAME` needs to match `class` attribute of template
|
||||
const NAME: &'static str = "LauncherWindow";
|
||||
type Type = super::Window;
|
||||
type ParentType = gtk::ApplicationWindow;
|
||||
|
||||
fn class_init(klass: &mut Self::Class) {
|
||||
Self::bind_template(klass);
|
||||
}
|
||||
|
||||
fn instance_init(obj: &InitializingObject<Self>) {
|
||||
obj.init_template();
|
||||
}
|
||||
}
|
||||
// Trait shared by all GObjects
|
||||
impl ObjectImpl for Window {
|
||||
fn constructed(&self, obj: &Self::Type) {
|
||||
// Call "constructed" on parent
|
||||
self.parent_constructed(obj);
|
||||
|
||||
// Setup
|
||||
obj.setup_model();
|
||||
obj.setup_callbacks();
|
||||
obj.setup_factory();
|
||||
}
|
||||
}
|
||||
// Trait shared by all widgets
|
||||
impl WidgetImpl for Window {}
|
||||
|
||||
// Trait shared by all windows
|
||||
impl WindowImpl for Window {}
|
||||
|
||||
// Trait shared by all application
|
||||
impl ApplicationWindowImpl for Window {}
|
||||
177
examples/dock/window/mod.rs
Normal file
177
examples/dock/window/mod.rs
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
mod imp;
|
||||
// use crate::ApplicationObject;
|
||||
use crate::TX;
|
||||
use crate::X11_CONN;
|
||||
use gdk4::Rectangle;
|
||||
use gdk4::Surface;
|
||||
use gdk4_x11::X11Surface;
|
||||
use gtk4 as gtk;
|
||||
use gtk4::Allocation;
|
||||
use postage::prelude::Sink;
|
||||
use x11rb::connection::Connection;
|
||||
use x11rb::protocol::xproto::ConnectionExt;
|
||||
|
||||
// use crate::application_row::ApplicationRow;
|
||||
use glib::Object;
|
||||
use gtk::prelude::*;
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{gio, glib};
|
||||
use gtk::{Application, SignalListItemFactory};
|
||||
use libcosmic::x;
|
||||
use x11rb::protocol::xproto;
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct Window(ObjectSubclass<imp::Window>)
|
||||
@extends gtk::ApplicationWindow, gtk::Window, gtk::Widget,
|
||||
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
|
||||
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
|
||||
}
|
||||
|
||||
const NUM_LAUNCHER_ITEMS: u8 = 9;
|
||||
|
||||
impl Window {
|
||||
pub fn new(app: &Application) -> Self {
|
||||
let self_: Self = Object::new(&[("application", app)]).expect("Failed to create `Window`.");
|
||||
self_
|
||||
}
|
||||
|
||||
pub fn model(&self) -> &gio::ListStore {
|
||||
// Get state
|
||||
let imp = imp::Window::from_instance(self);
|
||||
imp.model.get().expect("Could not get model")
|
||||
}
|
||||
|
||||
fn setup_model(&self) {
|
||||
// Get state and set model
|
||||
let imp = imp::Window::from_instance(self);
|
||||
// let model = gio::ListStore::new(ApplicationObject::static_type());
|
||||
|
||||
// let selection_model = gtk::SingleSelection::builder()
|
||||
// .model(&slice_model)
|
||||
// .autoselect(false)
|
||||
// .can_unselect(true)
|
||||
// .selected(gtk4::INVALID_LIST_POSITION)
|
||||
// .build();
|
||||
|
||||
// imp.model.set(model).expect("Could not set model");
|
||||
// // Wrap model with selection and pass it to the list view
|
||||
// imp.list_view.set_model(Some(&selection_model));
|
||||
}
|
||||
|
||||
fn setup_callbacks(&self) {
|
||||
// Get state
|
||||
let imp = imp::Window::from_instance(self);
|
||||
let window = self.clone().upcast::<gtk::Window>();
|
||||
let list_view = &imp.list_view;
|
||||
let lv = list_view.get();
|
||||
|
||||
// let app_selection_model = list_view
|
||||
// .model()
|
||||
// .expect("List view missing selection model")
|
||||
// .downcast::<gtk::SingleSelection>()
|
||||
// .expect("could not downcast listview model to single selection model");
|
||||
|
||||
// app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| {
|
||||
// let i = model.selected();
|
||||
// println!("acitvating... {}", i + 1);
|
||||
// let app_info = model.item(i);
|
||||
// if app_info.is_none() {
|
||||
// println!("oops no app for this row...");
|
||||
// return;
|
||||
// }
|
||||
// if let Ok(id) = app_info.unwrap().property("id") {
|
||||
// let id = id.get::<u32>().expect("App ID must be u32");
|
||||
|
||||
// glib::MainContext::default().spawn_local(async move {
|
||||
// if let Some(tx) = TX.get() {
|
||||
// let mut tx = tx.clone();
|
||||
// let _ = tx.send(crate::Event::Activate(id)).await;
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }));
|
||||
|
||||
window.connect_realize(move |window| {
|
||||
if let Some((display, surface)) = x::get_window_x11(window) {
|
||||
unsafe {
|
||||
x::change_property(
|
||||
&display,
|
||||
&surface,
|
||||
"_NET_WM_WINDOW_TYPE",
|
||||
x::PropMode::Replace,
|
||||
&[x::Atom::new(&display, "_NET_WM_WINDOW_TYPE_DOCK").unwrap()],
|
||||
);
|
||||
}
|
||||
let s = window.surface().unwrap();
|
||||
|
||||
s.connect_height_notify(glib::clone!(@weak window => move |s| {
|
||||
if let Some((display, _surface)) = x::get_window_x11(&window) {
|
||||
let width = s.width() * s.scale_factor();
|
||||
let height = s.height() * s.scale_factor();
|
||||
let monitor = display
|
||||
.monitor_at_surface(s)
|
||||
.expect("Failed to get Monitor");
|
||||
let Rectangle {
|
||||
x: _,
|
||||
y: _,
|
||||
width: monitor_width,
|
||||
height: monitor_height,
|
||||
} = monitor.geometry();
|
||||
dbg!(monitor_width);
|
||||
dbg!(monitor_height);
|
||||
dbg!(width);
|
||||
dbg!(height);
|
||||
let w_conf = xproto::ConfigureWindowAux::default()
|
||||
.x(monitor_width / 2 - width / 2)
|
||||
.y(monitor_height - height);
|
||||
let conn = X11_CONN.get().expect("Failed to get X11_CONN");
|
||||
|
||||
let x11surface = gdk4_x11::X11Surface::xid(
|
||||
&s.clone().downcast::<X11Surface>()
|
||||
.expect("Failed to downcast Surface to X11Surface"),
|
||||
);
|
||||
conn.configure_window(
|
||||
x11surface.try_into().expect("Failed to convert XID"),
|
||||
&w_conf,
|
||||
)
|
||||
.expect("failed to configure window...");
|
||||
conn.flush().expect("failed to flush");
|
||||
|
||||
} else {
|
||||
println!("failed to get X11 window");
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
println!("failed to get X11 window");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn setup_factory(&self) {
|
||||
let factory = SignalListItemFactory::new();
|
||||
// factory.connect_setup(move |_, list_item| {
|
||||
// let row = ApplicationRow::new():q ;
|
||||
// list_item.set_child(Some(&row))
|
||||
// });
|
||||
// factory.connect_bind(move |_, list_item| {
|
||||
// let application_object = list_item
|
||||
// .item()
|
||||
// .expect("The item has to exist.")
|
||||
// .downcast::<ApplicationObject>()
|
||||
// .expect("The item has to be an `ApplicationObject`");
|
||||
// let row = list_item
|
||||
// .child()
|
||||
// .expect("The list item child needs to exist.")
|
||||
// .downcast::<ApplicationRow>()
|
||||
// .expect("The list item type needs to be `ApplicationRow`");
|
||||
// if list_item.position() < 9 {
|
||||
// row.set_shortcut(list_item.position() + 1);
|
||||
// }
|
||||
|
||||
// row.set_app_info(application_object);
|
||||
// });
|
||||
// Set the factory of the list view
|
||||
let imp = imp::Window::from_instance(self);
|
||||
imp.list_view.set_factory(Some(&factory));
|
||||
}
|
||||
}
|
||||
19
examples/dock/window/window.ui
Normal file
19
examples/dock/window/window.ui
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<interface>
|
||||
<template class="LauncherWindow" parent="GtkApplicationWindow">
|
||||
<property name="height-request">64</property>
|
||||
<property name="width-request">256</property>
|
||||
<property name="title">Gtk Pop Dock</property>
|
||||
<property name="decorated">false</property>
|
||||
<property name="resizable">false</property>
|
||||
<child>
|
||||
<object class="GtkListView" id="list_view">
|
||||
<property name="orientation">horizontal</property>
|
||||
<property name="margin-top">4</property>
|
||||
<property name="margin-bottom">4</property>
|
||||
<property name="margin-start">4</property>
|
||||
<property name="margin-end">4</property>
|
||||
</object>
|
||||
</child>
|
||||
</template>
|
||||
</interface>
|
||||
Loading…
Add table
Add a link
Reference in a new issue