Skip to content

Commit ec0e5b5

Browse files
committed
moved mouse drag distance threshold into Theme so that it can be specified by the application
1 parent a40c48b commit ec0e5b5

File tree

5 files changed

+39
-33
lines changed

5 files changed

+39
-33
lines changed

src/events/global_input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ impl <'a> InputProvider<'a, GlobalInputEventIterator<'a>> for GlobalInput {
3838
impl GlobalInput {
3939

4040
/// Returns a fresh new `GlobalInput`
41-
pub fn new() -> GlobalInput {
41+
pub fn new(drag_threshold: Scalar) -> GlobalInput {
4242
GlobalInput{
4343
events: Vec::new(),
44-
drag_threshold: 4.0,
44+
drag_threshold: drag_threshold,
4545
start_state: InputState::new(),
4646
current_state: InputState::new(),
4747
}

src/tests/global_input.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use widget::{Id, Index};
99

1010
#[test]
1111
fn resetting_input_should_set_starting_state_to_current_state() {
12-
let mut input = GlobalInput::new();
12+
let mut input = GlobalInput::new(0.0);
1313
input.push_event(UiEvent::Raw(Input::Press(Keyboard(Key::LShift))));
1414
input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(0.0, 50.0))));
1515

@@ -20,7 +20,7 @@ fn resetting_input_should_set_starting_state_to_current_state() {
2020

2121
#[test]
2222
fn resetting_input_should_clear_out_all_events() {
23-
let mut input = GlobalInput::new();
23+
let mut input = GlobalInput::new(0.0);
2424
input.push_event(UiEvent::Raw(Input::Press(Keyboard(Key::LShift))));
2525
input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(0.0, 50.0))));
2626
input.reset();
@@ -29,7 +29,7 @@ fn resetting_input_should_clear_out_all_events() {
2929

3030
#[test]
3131
fn scroll_events_should_have_modifier_keys() {
32-
let mut input = GlobalInput::new();
32+
let mut input = GlobalInput::new(0.0);
3333

3434
input.push_event(UiEvent::Raw(Input::Press(Keyboard(Key::LShift))));
3535
input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(0.0, 50.0))));
@@ -40,7 +40,7 @@ fn scroll_events_should_have_modifier_keys() {
4040

4141
#[test]
4242
fn global_input_should_track_widget_currently_capturing_keyboard() {
43-
let mut input = GlobalInput::new();
43+
let mut input = GlobalInput::new(0.0);
4444

4545
let idx: Index = Index::Public(Id(5));
4646
input.push_event(UiEvent::WidgetCapturesKeyboard(idx));
@@ -57,7 +57,7 @@ fn global_input_should_track_widget_currently_capturing_keyboard() {
5757

5858
#[test]
5959
fn global_input_should_track_widget_currently_capturing_mouse() {
60-
let mut input = GlobalInput::new();
60+
let mut input = GlobalInput::new(0.0);
6161

6262
let idx: Index = Index::Public(Id(5));
6363
input.push_event(UiEvent::WidgetCapturesMouse(idx));
@@ -74,15 +74,15 @@ fn global_input_should_track_widget_currently_capturing_mouse() {
7474

7575
#[test]
7676
fn global_input_should_track_current_mouse_position() {
77-
let mut input = GlobalInput::new();
77+
let mut input = GlobalInput::new(0.0);
7878

7979
input.push_event(mouse_move_event(50.0, 77.7));
8080
assert_eq!([50.0, 77.7], input.mouse_position());
8181
}
8282

8383
#[test]
8484
fn entered_text_should_be_aggregated_from_multiple_events() {
85-
let mut input = GlobalInput::new();
85+
let mut input = GlobalInput::new(0.0);
8686

8787
input.push_event(UiEvent::Raw(Input::Text("Phil ".to_string())));
8888
input.push_event(UiEvent::Raw(Input::Text("is a".to_string())));
@@ -94,7 +94,7 @@ fn entered_text_should_be_aggregated_from_multiple_events() {
9494

9595
#[test]
9696
fn drag_event_should_still_be_created_if_reset_is_called_between_press_and_release() {
97-
let mut input = GlobalInput::new();
97+
let mut input = GlobalInput::new(4.0);
9898

9999
input.push_event(UiEvent::Raw(Input::Press(Mouse(MouseButton::Left))));
100100
input.push_event(mouse_move_event(50.0, 77.7));
@@ -106,7 +106,7 @@ fn drag_event_should_still_be_created_if_reset_is_called_between_press_and_relea
106106

107107
#[test]
108108
fn click_event_should_still_be_created_if_reset_is_called_between_press_and_release() {
109-
let mut input = GlobalInput::new();
109+
let mut input = GlobalInput::new(4.0);
110110

111111
input.push_event(UiEvent::Raw(Input::Press(Mouse(MouseButton::Left))));
112112
input.reset();
@@ -117,7 +117,7 @@ fn click_event_should_still_be_created_if_reset_is_called_between_press_and_rele
117117

118118
#[test]
119119
fn no_events_should_be_returned_after_reset_is_called() {
120-
let mut input = GlobalInput::new();
120+
let mut input = GlobalInput::new(0.0);
121121
input.push_event(UiEvent::Raw(Input::Press(Keyboard(Key::RShift))));
122122
input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(7.0, 88.5))));
123123
input.push_event(UiEvent::Raw(Input::Press(Mouse(MouseButton::Left))));
@@ -133,7 +133,7 @@ fn no_events_should_be_returned_after_reset_is_called() {
133133
fn drag_with_modifer_key_should_include_modifiers_in_drag_event() {
134134
use input::keyboard::SHIFT;
135135

136-
let mut input = GlobalInput::new();
136+
let mut input = GlobalInput::new(4.0);
137137
input.push_event(UiEvent::Raw(Input::Press(Keyboard(Key::RShift))));
138138
input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(7.0, 88.5))));
139139

@@ -146,7 +146,7 @@ fn drag_with_modifer_key_should_include_modifiers_in_drag_event() {
146146
fn click_with_modifier_key_should_include_modifiers_in_click_event() {
147147
use input::keyboard::CTRL;
148148

149-
let mut input = GlobalInput::new();
149+
let mut input = GlobalInput::new(4.0);
150150
input.push_event(UiEvent::Raw(Input::Press(Keyboard(Key::LCtrl))));
151151
input.push_event(UiEvent::Raw(Input::Press(Mouse(MouseButton::Left))));
152152
input.push_event(UiEvent::Raw(Input::Release(Mouse(MouseButton::Left))));
@@ -162,7 +162,7 @@ fn click_with_modifier_key_should_include_modifiers_in_click_event() {
162162

163163
#[test]
164164
fn high_level_scroll_event_should_be_created_from_a_raw_mouse_scroll() {
165-
let mut input = GlobalInput::new();
165+
let mut input = GlobalInput::new(0.0);
166166

167167
input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(10.0, 33.0))));
168168

@@ -177,7 +177,7 @@ fn high_level_scroll_event_should_be_created_from_a_raw_mouse_scroll() {
177177

178178
#[test]
179179
fn mouse_button_pressed_moved_released_creates_final_drag_event() {
180-
let mut input = GlobalInput::new();
180+
let mut input = GlobalInput::new(4.0);
181181

182182
input.push_event(UiEvent::Raw(Input::Press(Mouse(MouseButton::Left))));
183183
input.push_event(mouse_move_event(20.0, 10.0));
@@ -196,7 +196,7 @@ fn mouse_button_pressed_moved_released_creates_final_drag_event() {
196196

197197
#[test]
198198
fn mouse_button_pressed_then_moved_creates_drag_event() {
199-
let mut input = GlobalInput::new();
199+
let mut input = GlobalInput::new(4.0);
200200

201201
let press = UiEvent::Raw(Input::Press(Mouse(MouseButton::Left)));
202202
let mouse_move = mouse_move_event(20.0, 10.0);
@@ -216,7 +216,7 @@ fn mouse_button_pressed_then_moved_creates_drag_event() {
216216

217217
#[test]
218218
fn mouse_click_position_should_be_mouse_position_when_pressed() {
219-
let mut input = GlobalInput::new();
219+
let mut input = GlobalInput::new(4.0);
220220

221221
input.push_event(mouse_move_event(4.0, 5.0));
222222
input.push_event(UiEvent::Raw(Input::Press(Mouse(MouseButton::Left))));
@@ -236,7 +236,7 @@ fn mouse_click_position_should_be_mouse_position_when_pressed() {
236236

237237
#[test]
238238
fn mouse_button_pressed_then_released_should_create_mouse_click_event() {
239-
let mut input = GlobalInput::new();
239+
let mut input = GlobalInput::new(4.0);
240240

241241
let press = UiEvent::Raw(Input::Press(Mouse(MouseButton::Left)));
242242
let release = UiEvent::Raw(Input::Release(Mouse(MouseButton::Left)));
@@ -255,7 +255,7 @@ fn mouse_button_pressed_then_released_should_create_mouse_click_event() {
255255

256256
#[test]
257257
fn all_events_should_return_all_inputs_in_order() {
258-
let mut input = GlobalInput::new();
258+
let mut input = GlobalInput::new(0.0);
259259

260260
let evt1 = UiEvent::Raw(Input::Press(Keyboard(Key::Z)));
261261
input.push_event(evt1.clone());

src/tests/widget_input.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use position::Rect;
88
#[test]
99
fn maybe_mouse_position_should_return_position_if_mouse_is_over_the_widget() {
1010
let widget_area = Rect::from_corners([10.0, 10.0], [50.0, 50.0]);
11-
let mut global_input = GlobalInput::new();
11+
let mut global_input = GlobalInput::new(4.0);
1212
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseRelative(30.0, 30.0))));
1313

1414
let widget_input = WidgetInput::for_widget(Index::Public(Id(2)), widget_area, &global_input);
@@ -20,7 +20,7 @@ fn maybe_mouse_position_should_return_position_if_mouse_is_over_the_widget() {
2020
#[test]
2121
fn maybe_mouse_position_should_return_none_if_mouse_is_not_over_the_widget() {
2222
let widget_area = Rect::from_corners([10.0, 10.0], [50.0, 50.0]);
23-
let mut global_input = GlobalInput::new();
23+
let mut global_input = GlobalInput::new(4.0);
2424
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseRelative(-10.0, -10.0))));
2525

2626
let widget_input = WidgetInput::for_widget(Index::Public(Id(2)), widget_area, &global_input);
@@ -32,7 +32,7 @@ fn maybe_mouse_position_should_return_none_if_mouse_is_not_over_the_widget() {
3232
#[test]
3333
fn mouse_is_over_widget_should_be_true_if_mouse_is_over_the_widget_area() {
3434
let widget_area = Rect::from_corners([10.0, 10.0], [50.0, 50.0]);
35-
let mut global_input = GlobalInput::new();
35+
let mut global_input = GlobalInput::new(4.0);
3636
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseRelative(30.0, 30.0))));
3737

3838
let widget_input = WidgetInput::for_widget(Index::Public(Id(2)), widget_area, &global_input);
@@ -43,7 +43,7 @@ fn mouse_is_over_widget_should_be_true_if_mouse_is_over_the_widget_area() {
4343
#[test]
4444
fn mouse_is_over_widget_should_be_false_if_mouse_is_not_over_widget() {
4545
let widget_area = Rect::from_corners([10.0, 10.0], [50.0, 50.0]);
46-
let mut global_input = GlobalInput::new();
46+
let mut global_input = GlobalInput::new(4.0);
4747
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseRelative(90.0, 90.0))));
4848

4949
let widget_input = WidgetInput::for_widget(Index::Public(Id(2)), widget_area, &global_input);
@@ -54,7 +54,7 @@ fn mouse_is_over_widget_should_be_false_if_mouse_is_not_over_widget() {
5454
#[test]
5555
fn input_state_should_be_provided_relative_to_the_widget_area() {
5656
let widget_area = Rect::from_corners([10.0, 10.0], [50.0, 50.0]);
57-
let mut global_input = GlobalInput::new();
57+
let mut global_input = GlobalInput::new(4.0);
5858
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseRelative(30.0, 30.0))));
5959

6060
let widget_input = WidgetInput::for_widget(Index::Public(Id(2)), widget_area, &global_input);
@@ -64,7 +64,7 @@ fn input_state_should_be_provided_relative_to_the_widget_area() {
6464

6565
#[test]
6666
fn scroll_events_should_be_provided_if_widget_captures_mouse_but_not_keyboard() {
67-
let mut global_input = GlobalInput::new();
67+
let mut global_input = GlobalInput::new(4.0);
6868
let widget = Index::Public(Id(1));
6969
global_input.push_event(UiEvent::WidgetCapturesMouse(widget));
7070
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(0.0, -76.0))));
@@ -76,7 +76,7 @@ fn scroll_events_should_be_provided_if_widget_captures_mouse_but_not_keyboard()
7676

7777
#[test]
7878
fn scroll_events_should_be_provided_if_widget_captures_keyboard_but_not_mouse() {
79-
let mut global_input = GlobalInput::new();
79+
let mut global_input = GlobalInput::new(4.0);
8080
let widget = Index::Public(Id(1));
8181
global_input.push_event(UiEvent::WidgetCapturesKeyboard(widget));
8282
global_input.push_event(UiEvent::Raw(Input::Move(Motion::MouseScroll(0.0, -76.0))));
@@ -88,7 +88,7 @@ fn scroll_events_should_be_provided_if_widget_captures_keyboard_but_not_mouse()
8888

8989
#[test]
9090
fn widget_input_should_provide_any_mouse_events_over_the_widgets_area_if_nothing_is_capturing_mouse() {
91-
let mut global_input = GlobalInput::new();
91+
let mut global_input = GlobalInput::new(4.0);
9292
global_input.push_event(UiEvent::MouseClick(MouseClick{
9393
button: MouseButton::Left,
9494
location: [10.0, 10.0],
@@ -111,7 +111,7 @@ fn widget_input_should_provide_any_mouse_events_over_the_widgets_area_if_nothing
111111

112112
#[test]
113113
fn widget_input_should_only_provide_keyboard_input_to_widget_that_has_focus() {
114-
let mut global_input = GlobalInput::new();
114+
let mut global_input = GlobalInput::new(4.0);
115115

116116
let some_rect = Rect::from_corners([0.0, 0.0], [40.0, 40.0]);
117117
let widget_4 = Index::Public(Id(4));
@@ -131,7 +131,7 @@ fn widget_input_should_only_provide_keyboard_input_to_widget_that_has_focus() {
131131
#[test]
132132
fn mouse_clicks_should_be_relative_to_widget_position() {
133133
let idx = Index::Public(Id(5));
134-
let mut global_input = GlobalInput::new();
134+
let mut global_input = GlobalInput::new(4.0);
135135
global_input.push_event(UiEvent::MouseClick(MouseClick{
136136
button: MouseButton::Left,
137137
location: [10.0, 10.0],
@@ -149,7 +149,7 @@ fn mouse_drags_should_be_relative_to_widget_position() {
149149
use events::MouseDrag;
150150

151151
let idx = Index::Public(Id(5));
152-
let mut global_input = GlobalInput::new();
152+
let mut global_input = GlobalInput::new(4.0);
153153
global_input.push_event(UiEvent::MouseDrag(MouseDrag{
154154
button: MouseButton::Left,
155155
start: [5.0, 5.0],

src/theme.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub struct Theme {
4040
pub maybe_scrollbar: Option<widget::scroll::Style>,
4141
/// Unique styling for each widget, index-able by the **Widget::kind**.
4242
pub widget_styling: HashMap<&'static str, WidgetDefault>,
43+
/// Mouse Drag distance threshold determines the minimum distance from the mouse-down point
44+
/// that the mouse must move before starting a drag operation.
45+
pub mouse_drag_threshold: Scalar,
4346
}
4447

4548
/// The defaults for a specific widget.
@@ -69,6 +72,8 @@ impl WidgetDefault {
6972
}
7073
}
7174

75+
/// This is the default value that is used for `Theme::mouse_drag_threshold`.
76+
pub const DEFAULT_MOUSE_DRAG_THRESHOLD: Scalar = 4.0;
7277

7378
impl Theme {
7479

@@ -89,6 +94,7 @@ impl Theme {
8994
font_size_small: 12,
9095
maybe_scrollbar: None,
9196
widget_styling: HashMap::new(),
97+
mouse_drag_threshold: DEFAULT_MOUSE_DRAG_THRESHOLD,
9298
}
9399
}
94100

@@ -110,4 +116,3 @@ impl Theme {
110116
}
111117

112118
}
113-

src/ui.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ impl<C> Ui<C> {
148148
{
149149
let window = widget_graph.add_placeholder();
150150
let prev_updated_widgets = updated_widgets.clone();
151+
let mouse_drag_threshold = theme.mouse_drag_threshold;
151152
Ui {
152153
widget_graph: widget_graph,
153154
theme: theme,
@@ -171,7 +172,7 @@ impl<C> Ui<C> {
171172
depth_order: depth_order,
172173
updated_widgets: updated_widgets,
173174
prev_updated_widgets: prev_updated_widgets,
174-
global_input: GlobalInput::new(),
175+
global_input: GlobalInput::new(mouse_drag_threshold),
175176
}
176177
}
177178

0 commit comments

Comments
 (0)