Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input: Add a scroll_factor config option #155

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosmic-comp-config/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct ScrollConfig {
pub method: Option<ScrollMethod>,
pub natural_scroll: Option<bool>,
pub scroll_button: Option<u32>,
pub scroll_factor: Option<f64>,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion src/config/input_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn for_device(device: &InputDevice) -> InputConfig {
} else {
None
},
scroll_factor: None,
})
} else {
None
Expand All @@ -83,7 +84,7 @@ pub fn for_device(device: &InputDevice) -> InputConfig {

// Get setting from `device_config` if present, then `default_config`
// Returns `is_default` to indicate this is a default value.
fn get_config<'a, T: 'a, F: Fn(&'a InputConfig) -> Option<T>>(
pub fn get_config<'a, T: 'a, F: Fn(&'a InputConfig) -> Option<T>>(
device_config: Option<&'a InputConfig>,
default_config: &'a InputConfig,
f: F,
Expand Down
15 changes: 14 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,26 @@ impl Config {
}

pub fn read_device(&self, device: &mut InputDevice) {
let (device_config, default_config) = self.get_device_config(device);
input_config::update_device(device, device_config, default_config);
}

pub fn scroll_factor(&self, device: &InputDevice) -> f64 {
let (device_config, default_config) = self.get_device_config(device);
input_config::get_config(device_config, default_config, |x| {
x.scroll_config.as_ref()?.scroll_factor
})
.map_or(1.0, |x| x.0)
}

fn get_device_config(&self, device: &InputDevice) -> (Option<&InputConfig>, &InputConfig) {
let default_config = if device.config_tap_finger_count() > 0 {
&self.input_touchpad
} else {
&self.input_default
};
let device_config = self.input_devices.get(device.name());
input_config::update_device(device, device_config, default_config);
(device_config, default_config)
}
}

Expand Down
26 changes: 22 additions & 4 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
};
use calloop::{timer::Timer, RegistrationToken};
use cosmic_protocols::screencopy::v1::server::zcosmic_screencopy_session_v1::InputType;
#[allow(deprecated)]
use smithay::{
backend::input::{
Axis, AxisSource, Device, DeviceCapability, InputBackend, InputEvent, KeyState,
Expand All @@ -27,7 +28,10 @@ use smithay::{
Seat, SeatState,
},
output::Output,
reexports::wayland_server::DisplayHandle,
reexports::{
input::event::pointer::PointerAxisEvent as LibinputPointerAxisEvent,
wayland_server::DisplayHandle,
},
utils::{Logical, Point, Rectangle, Serial, SERIAL_COUNTER},
wayland::{
keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitorSeat, seat::WaylandFocus,
Expand All @@ -40,6 +44,7 @@ use tracing::info;
use tracing::{error, trace, warn};

use std::{
any::Any,
cell::RefCell,
collections::HashMap,
time::{Duration, Instant},
Expand Down Expand Up @@ -171,7 +176,9 @@ impl State {
&mut self,
event: InputEvent<B>,
needs_key_repetition: bool,
) {
) where
<B as InputBackend>::PointerAxisEvent: 'static,
{
use smithay::backend::input::Event;

match event {
Expand Down Expand Up @@ -737,6 +744,15 @@ impl State {
}
}
InputEvent::PointerAxis { event, .. } => {
#[allow(deprecated)]
let scroll_factor = if let Some(event) =
<dyn Any>::downcast_ref::<LibinputPointerAxisEvent>(&event)
{
self.common.config.scroll_factor(&event.device())
} else {
1.0
};

let device = event.device();
for seat in self.common.seats().cloned().collect::<Vec<_>>().iter() {
#[cfg(feature = "debug")]
Expand Down Expand Up @@ -775,15 +791,17 @@ impl State {
let mut frame =
AxisFrame::new(event.time_msec()).source(event.source());
if horizontal_amount != 0.0 {
frame = frame.value(Axis::Horizontal, horizontal_amount);
frame = frame
.value(Axis::Horizontal, scroll_factor * horizontal_amount);
if let Some(discrete) = horizontal_amount_discrete {
frame = frame.discrete(Axis::Horizontal, discrete as i32);
}
} else if event.source() == AxisSource::Finger {
frame = frame.stop(Axis::Horizontal);
}
if vertical_amount != 0.0 {
frame = frame.value(Axis::Vertical, vertical_amount);
frame =
frame.value(Axis::Vertical, scroll_factor * vertical_amount);
if let Some(discrete) = vertical_amount_discrete {
frame = frame.discrete(Axis::Vertical, discrete as i32);
}
Expand Down
Loading