Skip to content

Commit

Permalink
wip allow disabling kinetic scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
misson20000 committed Aug 3, 2024
1 parent 8bcfa8b commit 1d8963f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ default = ["gtk"]
gtk = ["dep:gtk"]

[dependencies]
gtk = { version = "0.9.0", optional = true, package = "gtk4" }
gtk = { version = "0.9.0", optional = true, package = "gtk4", features = ["v4_8"] }
glib = "0.20.0"
futures = "0.3.30"
lazy_static = "1.5.0"
Expand Down
10 changes: 6 additions & 4 deletions src/view/breadcrumbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,22 @@ impl CharmBreadcrumbWidget {

pub fn list_item_factory(lw: listing::ListingWidget) -> gtk::SignalListItemFactory {
let slif = gtk::SignalListItemFactory::new();
slif.connect_setup(move |_, obj| {
slif.connect_setup(move |_, obj| catch_panic! {
let obj = obj.downcast_ref::<gtk::ListItem>().unwrap();
let lw = lw.clone();
catch_panic! {
obj.set_child(Some(&Self::new(lw)));
}
obj.set_child(Some(&Self::new(lw)));
obj.set_activatable(false);
});
slif.connect_bind(|_, obj| catch_panic! {
let obj = obj.downcast_ref::<gtk::ListItem>().unwrap();
obj.child().unwrap().downcast::<Self>().unwrap().bind(Some(obj.item().unwrap().downcast::<CharmBreadcrumb>().unwrap()));
});
slif.connect_unbind(|_, obj| catch_panic! {
let obj = obj.downcast_ref::<gtk::ListItem>().unwrap();
obj.child().unwrap().downcast::<Self>().unwrap().bind(None);
});
slif.connect_teardown(|_, obj| catch_panic! {
let obj = obj.downcast_ref::<gtk::ListItem>().unwrap();
obj.set_child(gtk::Widget::NONE);
});
slif
Expand Down
64 changes: 61 additions & 3 deletions src/view/charm.cmb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,16 @@
(11,39,"GtkBox",None,7,None,None,None,None,None,None),
(11,40,"GtkLabel",None,39,None,None,None,None,None,None),
(11,41,"GtkSwitch","dark-mode",39,None,None,None,1,None,None),
(11,42,"GtkAdjustment","indentation-width",11,None,None,None,-1,None,None)
(11,42,"GtkAdjustment","indentation-width",11,None,None,None,-1,None,None),
(11,43,"GtkGrid",None,5,None,None,None,-1,None,None),
(11,44,"GtkLabel",None,43,None,None,None,None,None,None),
(11,45,"GtkLabel",None,43,None,None,None,None,None,None),
(11,46,"GtkSpinButton",None,43,None,None,None,None,None,None),
(11,47,"GtkSpinButton",None,43,None,None,None,1,None,None),
(11,48,"GtkAdjustment","scroll-wheel-impulse",46,None,None,None,-1,None,None),
(11,49,"GtkAdjustment","scroll-deceleration",47,None,None,None,-1,None,None),
(11,50,"GtkLabel",None,43,None,None,None,None,None,None),
(11,51,"GtkSwitch","scroll-enable-kinetic",43,None,None,None,2,None,None)
</object>
<object_property>
(2,20,"GtkGrid","column-spacing","30",None,None,None,None,None,None,None,None,None),
Expand Down Expand Up @@ -411,6 +420,7 @@
(11,3,"GtkWidget","vexpand","True",None,None,None,None,None,None,None,None,None),
(11,4,"GtkStackPage","child",None,None,None,None,None,7,None,None,None,None),
(11,4,"GtkStackPage","title","Style",None,None,None,None,None,None,None,None,None),
(11,5,"GtkStackPage","child",None,None,None,None,None,43,None,None,None,None),
(11,5,"GtkStackPage","title","Scrolling",None,None,None,None,None,None,None,None,None),
(11,6,"GtkStackPage","title","Advanced",None,None,None,None,None,None,None,None,None),
(11,7,"GtkGrid","column-spacing","20",None,None,None,None,None,None,None,None,None),
Expand Down Expand Up @@ -459,7 +469,31 @@
(11,42,"GtkAdjustment","page-increment","1.0",None,None,None,None,None,None,None,None,None),
(11,42,"GtkAdjustment","step-increment","1.0",None,None,None,None,None,None,None,None,None),
(11,42,"GtkAdjustment","upper","16.0",None,None,None,None,None,None,None,None,None),
(11,42,"GtkAdjustment","value","2.0",None,None,None,None,None,None,None,None,None)
(11,42,"GtkAdjustment","value","2.0",None,None,None,None,None,None,None,None,None),
(11,43,"GtkGrid","column-spacing","20",None,None,None,None,None,None,None,None,None),
(11,43,"GtkGrid","row-spacing","5",None,None,None,None,None,None,None,None,None),
(11,43,"GtkWidget","halign","center",None,None,None,None,None,None,None,None,None),
(11,43,"GtkWidget","margin-bottom","10",None,None,None,None,None,None,None,None,None),
(11,43,"GtkWidget","margin-end","10",None,None,None,None,None,None,None,None,None),
(11,43,"GtkWidget","margin-start","10",None,None,None,None,None,None,None,None,None),
(11,43,"GtkWidget","margin-top","10",None,None,None,None,None,None,None,None,None),
(11,43,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None),
(11,44,"GtkLabel","label","Wheel Impulse",None,None,None,None,None,None,None,None,None),
(11,45,"GtkLabel","label","Deceleration",None,None,None,None,None,None,None,None,None),
(11,46,"GtkSpinButton","adjustment",None,None,None,None,None,48,None,None,None,None),
(11,46,"GtkWidget","hexpand","True",None,None,None,None,None,None,None,None,None),
(11,47,"GtkSpinButton","adjustment",None,None,None,None,None,49,None,None,None,None),
(11,47,"GtkWidget","hexpand","True",None,None,None,None,None,None,None,None,None),
(11,48,"GtkAdjustment","page-size","10.0",None,None,None,None,None,None,None,None,None),
(11,48,"GtkAdjustment","step-increment","1.0",None,None,None,None,None,None,None,None,None),
(11,48,"GtkAdjustment","upper","400.0",None,None,None,None,None,None,None,None,None),
(11,48,"GtkAdjustment","value","60.0",None,None,None,None,None,None,None,None,None),
(11,49,"GtkAdjustment","page-size","100.0",None,None,None,None,None,None,None,None,None),
(11,49,"GtkAdjustment","step-increment","10.0",None,None,None,None,None,None,None,None,None),
(11,49,"GtkAdjustment","upper","1000.0",None,None,None,None,None,None,None,None,None),
(11,49,"GtkAdjustment","value","400.0",None,None,None,None,None,None,None,None,None),
(11,50,"GtkLabel","label","Kinetic Scrolling",None,None,None,None,None,None,None,None,None),
(11,51,"GtkWidget","halign","start",None,None,None,None,None,None,None,None,None)
</object_property>
<object_layout_property>
(2,20,21,"GtkGridLayoutChild","column","0",None,None,None,None),
Expand Down Expand Up @@ -703,7 +737,31 @@
(11,7,39,"GtkGridLayoutChild","column","0",None,None,None,None),
(11,7,39,"GtkGridLayoutChild","column-span","3",None,None,None,None),
(11,7,39,"GtkGridLayoutChild","row","2",None,None,None,None),
(11,7,39,"GtkGridLayoutChild","row-span","1",None,None,None,None)
(11,7,39,"GtkGridLayoutChild","row-span","1",None,None,None,None),
(11,43,44,"GtkGridLayoutChild","column","0",None,None,None,None),
(11,43,44,"GtkGridLayoutChild","column-span","1",None,None,None,None),
(11,43,44,"GtkGridLayoutChild","row","0",None,None,None,None),
(11,43,44,"GtkGridLayoutChild","row-span","1",None,None,None,None),
(11,43,45,"GtkGridLayoutChild","column","0",None,None,None,None),
(11,43,45,"GtkGridLayoutChild","column-span","1",None,None,None,None),
(11,43,45,"GtkGridLayoutChild","row","1",None,None,None,None),
(11,43,45,"GtkGridLayoutChild","row-span","1",None,None,None,None),
(11,43,46,"GtkGridLayoutChild","column","1",None,None,None,None),
(11,43,46,"GtkGridLayoutChild","column-span","1",None,None,None,None),
(11,43,46,"GtkGridLayoutChild","row","0",None,None,None,None),
(11,43,46,"GtkGridLayoutChild","row-span","1",None,None,None,None),
(11,43,47,"GtkGridLayoutChild","column","1",None,None,None,None),
(11,43,47,"GtkGridLayoutChild","column-span","1",None,None,None,None),
(11,43,47,"GtkGridLayoutChild","row","1",None,None,None,None),
(11,43,47,"GtkGridLayoutChild","row-span","1",None,None,None,None),
(11,43,50,"GtkGridLayoutChild","column","0",None,None,None,None),
(11,43,50,"GtkGridLayoutChild","column-span","1",None,None,None,None),
(11,43,50,"GtkGridLayoutChild","row","2",None,None,None,None),
(11,43,50,"GtkGridLayoutChild","row-span","1",None,None,None,None),
(11,43,51,"GtkGridLayoutChild","column","1",None,None,None,None),
(11,43,51,"GtkGridLayoutChild","column-span","1",None,None,None,None),
(11,43,51,"GtkGridLayoutChild","row","2",None,None,None,None),
(11,43,51,"GtkGridLayoutChild","row-span","1",None,None,None,None)
</object_layout_property>
<object_signal>
(2,8,6,"GtkButton","clicked","handle_save_as",None,None,1,None,None)
Expand Down
28 changes: 27 additions & 1 deletion src/view/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,16 @@ declare_config![Config {
file_access_delay: u64 = 0, /* milliseconds */

lookahead: usize = 20, /* lines */

#[bind("scroll-enable-kinetic")]
scroll_enable_kinetic: bool = true,

#[bind("scroll-wheel-impulse")]
scroll_wheel_impulse: f64 = 60.0, /* lines/second */

#[bind("scroll-deceleration")]
scroll_deceleration: f64 = 400.0, /* lines/second^2 */

scroll_spring: f64 = 240.0, /* 1/second^2 */
scroll_spring_damping: f64 = 17.0, /* viscous damping coefficient */

Expand Down Expand Up @@ -560,7 +567,8 @@ impl Item for bool {
}

fn update_binding(&self, binding: &Self::Binding) {
binding.set_state(*self)
binding.set_state(*self);
binding.set_active(*self);
}
}

Expand All @@ -581,3 +589,21 @@ impl Item for f32 {
binding.set_value(*self as f64)
}
}

impl Item for f64 {
type Binding = gtk::Adjustment;

fn bind<F: Fn(Self) + Clone + 'static>(builder: &gtk::Builder, id: &str, changer: F) -> Self::Binding {
let adj = builder.object::<gtk::Adjustment>(id).unwrap();

adj.connect_value_changed(move |adj| {
changer(adj.value());
});

adj
}

fn update_binding(&self, binding: &Self::Binding) {
binding.set_value(*self)
}
}
10 changes: 7 additions & 3 deletions src/view/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,16 @@ impl ListingWidget {
self.add_controller(ec_key);

/* Register scroll event controller */
let ec_scroll = gtk::EventControllerScroll::new(gtk::EventControllerScrollFlags::VERTICAL);
ec_scroll.connect_scroll(clone!(#[weak(rename_to=lw)] self, #[upgrade_or] glib::Propagation::Proceed, move |_ecs, dx, dy| catch_panic! {
let ec_scroll = gtk::EventControllerScroll::new(gtk::EventControllerScrollFlags::VERTICAL | gtk::EventControllerScrollFlags::KINETIC);
ec_scroll.connect_scroll(clone!(#[weak(rename_to=lw)] self, #[upgrade_or] glib::Propagation::Proceed, move |ecs, dx, dy| catch_panic! {
@default(glib::Propagation::Proceed);


println!("got scroll event, ({}, {}) unit {:?}", dx, dy, ecs.unit());
lw.imp().interior.get().unwrap().write().scroll(&lw, dx, dy)
}));
ec_scroll.connect_decelerate(clone!(#[weak(rename_to=lw)] self, move |ecs, vx, vy| catch_panic! {
println!("got decelerate event, ({}, {})", vx, vy);
}));
self.add_controller(ec_scroll);

/* Register motion event controller for hovering */
Expand Down
10 changes: 7 additions & 3 deletions src/view/listing/facet/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ impl Scroller {
}

pub fn scroll_wheel_impulse(&mut self, delta: f64) {
self.velocity+= delta * self.config.scroll_wheel_impulse;
if self.config.scroll_enable_kinetic {
self.velocity+= delta * self.config.scroll_wheel_impulse;
} else {
self.position+= delta;
}
self.bonked_top = false;
self.bonked_bottom = false;

Expand All @@ -78,7 +82,7 @@ impl Scroller {
self.ev_work.want();
} else {
/* we are now bonked on the bottom... */
if self.velocity > 0.0 {
if self.velocity > 0.0 || !self.config.scroll_enable_kinetic {
self.bonked_bottom = true;
}
break;
Expand All @@ -98,7 +102,7 @@ impl Scroller {
self.ev_work.want();
} else {
/* we are now bonked on the top... */
if self.velocity < 0.0 && self.position < 1.0 {
if (self.velocity < 0.0 || !self.config.scroll_enable_kinetic) && self.position < 0.0 {
self.bonked_top = true;
}
break;
Expand Down
93 changes: 91 additions & 2 deletions src/view/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,97 @@
<child>
<object class="GtkStackPage">
<property name="child">
<object class="GtkLabel">
<property name="label">Empty Page</property>
<object class="GtkGrid">
<property name="column-spacing">20</property>
<property name="halign">center</property>
<property name="margin-bottom">10</property>
<property name="margin-end">10</property>
<property name="margin-start">10</property>
<property name="margin-top">10</property>
<property name="row-spacing">5</property>
<property name="valign">center</property>
<child>
<object class="GtkLabel">
<property name="label">Wheel Impulse</property>
<layout>
<property name="column">0</property>
<property name="column-span">1</property>
<property name="row">0</property>
<property name="row-span">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">Deceleration</property>
<layout>
<property name="column">0</property>
<property name="column-span">1</property>
<property name="row">1</property>
<property name="row-span">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkSpinButton">
<property name="adjustment">
<object class="GtkAdjustment" id="scroll-wheel-impulse">
<property name="page-size">10.0</property>
<property name="step-increment">1.0</property>
<property name="upper">400.0</property>
<property name="value">60.0</property>
</object>
</property>
<property name="hexpand">True</property>
<layout>
<property name="column">1</property>
<property name="column-span">1</property>
<property name="row">0</property>
<property name="row-span">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label">Kinetic Scrolling</property>
<layout>
<property name="column">0</property>
<property name="column-span">1</property>
<property name="row">2</property>
<property name="row-span">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkSpinButton">
<property name="adjustment">
<object class="GtkAdjustment" id="scroll-deceleration">
<property name="page-size">100.0</property>
<property name="step-increment">10.0</property>
<property name="upper">1000.0</property>
<property name="value">400.0</property>
</object>
</property>
<property name="hexpand">True</property>
<layout>
<property name="column">1</property>
<property name="column-span">1</property>
<property name="row">1</property>
<property name="row-span">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkSwitch" id="scroll-enable-kinetic">
<property name="halign">start</property>
<layout>
<property name="column">1</property>
<property name="column-span">1</property>
<property name="row">2</property>
<property name="row-span">1</property>
</layout>
</object>
</child>
</object>
</property>
<property name="title">Scrolling</property>
Expand Down

0 comments on commit 1d8963f

Please sign in to comment.