Skip to content

Commit

Permalink
fix: FINALLY! FIXED BRO!
Browse files Browse the repository at this point in the history
- Fixed scrolling
  • Loading branch information
PiotrekPKP committed May 21, 2022
1 parent d6570b9 commit b3dc002
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub const KEY_ESC: u32 = 9;
pub const KEY_LEFT_SHIFT: u32 = 50;
pub const KEY_RIGHT_SHIFT: u32 = 62;
pub const KEY_ENTER: u32 = 36;

pub const ITEMS_PER_PAGE: u32 = 9;
1 change: 0 additions & 1 deletion src/resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ row {
background: @bg-color;
margin: 0;
padding: 0;
height: 15px;
}

row:selected > box {
Expand Down
5 changes: 2 additions & 3 deletions src/resources/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="margin-top">24</property>
<property name="margin-bottom">12</property>
<property name="margin-bottom">24</property>
<property name="margin-start">0</property>
<property name="margin-end">0</property>
<property name="spacing">24</property>
Expand All @@ -31,9 +31,8 @@
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<object class="GtkScrolledWindow" id="scrolled_window">
<property name="hscrollbar-policy">never</property>
<property name="min-content-height">360</property>
<property name="vexpand">true</property>
<child>
<object class="GtkListView" id="crab_items_list" />
Expand Down
4 changes: 3 additions & 1 deletion src/window/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib, ScrolledWindow};
use gtk::{CompositeTemplate, Entry, ListView};
use std::cell::{Cell, RefCell};
use std::cell::{RefCell};

#[derive(CompositeTemplate, Default)]
#[template(resource = "/wm/crab/launcher/window.ui")]
pub struct Window {
#[template_child]
pub entry: TemplateChild<Entry>,
#[template_child]
pub scrolled_window: TemplateChild<ScrolledWindow>,
#[template_child]
pub crab_items_list: TemplateChild<ListView>,
pub current_items: RefCell<Option<gio::ListStore>>,
}
Expand Down
69 changes: 33 additions & 36 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use crate::crab_row::CrabRow;
use gtk::glib::{clone, MainContext, Object};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{CustomFilter, FilterListModel, gio, Inhibit, SignalListItemFactory, SingleSelection};
use gtk::{Adjustment, CustomFilter, FilterListModel, gio, Inhibit, ScrollType, SignalListItemFactory, SingleSelection};
use gtk::{glib, Application, FilterChange};
use gtk::gio::{AppInfo};
use gtk::gdk::AppLaunchContext;

mod imp;

Expand Down Expand Up @@ -67,50 +66,43 @@ impl Window {
}));

self.imp().entry.connect_activate(clone!(@weak self as window, @weak selection_model => move |_| {
let list_view = &window.imp().crab_items_list;

let model = list_view.model().unwrap();
let app_info = model
.item(selection_model.selected())
.unwrap()
.downcast::<gio::AppInfo>()
.unwrap();

let parent_window = list_view.root().unwrap().downcast::<gtk::Window>().unwrap();
let context = gtk::Window::new().display().app_launch_context();

window.hide();
open_app(&app_info, &parent_window, &context);
open_app(
&selection_model.selected_item().unwrap().downcast::<AppInfo>().unwrap(),
&window,
);
}));

let controller = gtk::EventControllerKey::new();
controller.connect_key_pressed(clone!(@strong self as window => move |_, key, keycode, _| {
match keycode {
KEY_UP_ARROW => {
selection_model.select_item(selection_model.selected() - 1, true);
let new_selection = if selection_model.selected() > 0 { selection_model.selected() - 1 } else { 0 };
selection_model.select_item(new_selection, true);
window.imp().crab_items_list.activate_action("list.scroll-to-item", Some(&new_selection.to_variant())).unwrap();

Inhibit(false)
}
KEY_DOWN_ARROW => {
selection_model.select_item(selection_model.selected() + 1, true);
let new_selection = std::cmp::min(selection_model.n_items() - 1, selection_model.selected() + 1);
selection_model.select_item(new_selection, true);
window.imp().crab_items_list.activate_action("list.scroll-to-item", Some(&new_selection.to_variant())).unwrap();

Inhibit(false)
}
KEY_ESC => {
window.close();

Inhibit(false)
}
KEY_ENTER => {
let model = window.imp().crab_items_list.model().unwrap();
let app_info = model
.item(selection_model.selected())
.unwrap()
.downcast::<gio::AppInfo>()
.unwrap();

let parent_window = window.imp().crab_items_list.root().unwrap().downcast::<gtk::Window>().unwrap();
let context = gtk::Window::new().display().app_launch_context();

window.hide();
open_app(&app_info, &parent_window, &context);
open_app(
&selection_model.selected_item().unwrap().downcast::<AppInfo>().unwrap(),
&window,
);

Inhibit(false)
}
_ => {
dbg!(keycode);
if keycode != KEY_LEFT_SHIFT && keycode != KEY_RIGHT_SHIFT {
window.imp().entry.grab_focus();
}
Expand All @@ -127,10 +119,10 @@ impl Window {
window.imp().entry.set_placeholder_text(None);
}
}

Inhibit(false)
}
}

Inhibit(false)
}));

self.add_controller(&controller);
Expand Down Expand Up @@ -160,19 +152,24 @@ impl Window {
}
}

fn open_app(app_info: &AppInfo, parent_window: &gtk::Window, context: &AppLaunchContext) {
fn open_app(app_info: &AppInfo, window: &Window) {
let parent_window = window.imp().crab_items_list.root().unwrap().downcast::<gtk::Window>().unwrap();
let context = gtk::Window::new().display().app_launch_context();

window.hide();

let commandline = app_info.commandline().unwrap();
let main_context = MainContext::default();

main_context.spawn_local(clone!(@strong commandline, @strong parent_window, @strong app_info, @strong context => async move {
main_context.spawn_local(clone!(@strong commandline, @strong window, @strong app_info, @strong context => async move {
if let Err(_) = async_process::Command::new(commandline.as_os_str()).output().await {
if let Err(err) = app_info.launch(&[], Some(&context)) {
gtk::MessageDialog::builder()
.text(&format!("Failed to start {}!", app_info.name()))
.secondary_text(&err.to_string())
.message_type(gtk::MessageType::Error)
.modal(true)
.transient_for(&parent_window)
.transient_for(&window)
.build()
.show();
}
Expand Down

0 comments on commit b3dc002

Please sign in to comment.