pub struct Interval { /* private fields */ }
Expand description
The main struct for the interval system.
Accessed via basalt.interval_ref()
or basalt.interval()
.
Implementations§
source§impl Interval
impl Interval
sourcepub fn do_every<F: FnMut(Option<Duration>) -> IntvlHookCtrl + Send + 'static>(
&self,
every: Duration,
delay: Option<Duration>,
method: F
) -> IntvlHookID
pub fn do_every<F: FnMut(Option<Duration>) -> IntvlHookCtrl + Send + 'static>(
&self,
every: Duration,
delay: Option<Duration>,
method: F
) -> IntvlHookID
Call the method at provided internval.
Takes a Fn(last_call: Option<Duration>) -> IntvlHookCtrl
.
last_call
: Duration since the last method was called.delay
: is the duration that has to elapsed afterInterval::start(...)
before the hook method is called.IntvlHookCtrl
: controls how the hook is handled after the method is called.
Notes
- Hooks are paused to begin with. They must be started with
Interval::start(...)
. last_call
will only beSome
if the method is called continuously. ReturningInputHookCtrl::Pause
or usingInterval::pause(...)
will cause the next call to beNone
.
Examples found in repository?
src/interface/bin/mod.rs (lines 870-892)
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
pub fn fade_out(self: &Arc<Self>, millis: u64) {
let bin_wk = Arc::downgrade(self);
let start_opacity = self.style_copy().opacity.unwrap_or(1.0);
let steps = (millis / 8) as i64;
let step_size = start_opacity / steps as f32;
let mut step_i = 0;
self.basalt
.interval_ref()
.do_every(Duration::from_millis(8), None, move |_| {
if step_i > steps {
return IntvlHookCtrl::Remove;
}
let bin = match bin_wk.upgrade() {
Some(some) => some,
None => return IntvlHookCtrl::Remove,
};
let opacity = start_opacity - (step_i as f32 * step_size);
let mut copy = bin.style_copy();
copy.opacity = Some(opacity);
if step_i == steps {
copy.hidden = Some(true);
}
bin.style_update(copy).expect_valid();
bin.update_children();
step_i += 1;
Default::default()
});
}
pub fn fade_in(self: &Arc<Self>, millis: u64, target: f32) {
let bin_wk = Arc::downgrade(self);
let start_opacity = self.style_copy().opacity.unwrap_or(1.0);
let steps = (millis / 8) as i64;
let step_size = (target - start_opacity) / steps as f32;
let mut step_i = 0;
self.basalt
.interval_ref()
.do_every(Duration::from_millis(8), None, move |_| {
if step_i > steps {
return IntvlHookCtrl::Remove;
}
let bin = match bin_wk.upgrade() {
Some(some) => some,
None => return IntvlHookCtrl::Remove,
};
let opacity = (step_i as f32 * step_size) + start_opacity;
let mut copy = bin.style_copy();
copy.opacity = Some(opacity);
copy.hidden = Some(false);
bin.style_update(copy).expect_valid();
bin.update_children();
step_i += 1;
Default::default()
});
}
More examples
src/input/builder.rs (lines 362-380)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
pub fn finish(mut self) -> Result<InputHookID, InputError> {
if self.keys.is_empty() {
Err(InputError::NoKeys)
} else if self.method.is_none() {
Err(InputError::NoMethod)
} else if self.parent.target == InputHookTarget::None {
Err(InputError::NoTarget)
} else {
let state = LocalKeyState::from_keys(self.keys);
let mut local = state.clone();
local.press_all();
let event_send = self.parent.input.event_send();
let interval = self.parent.input.interval();
let input_hook_id = self.parent.input.next_id();
let mut method = self.method.take().unwrap();
let target_wk = self.parent.target.weak();
let intvl_id = interval.do_every(self.intvl, self.delay, move |last_call| {
match target_wk.upgrade() {
Some(target) => {
match method(target, &local, last_call) {
InputHookCtrl::Retain | InputHookCtrl::RetainNoPass => {
IntvlHookCtrl::Continue
},
InputHookCtrl::Remove | InputHookCtrl::RemoveNoPass => {
event_send.send(LoopEvent::Remove(input_hook_id)).unwrap();
IntvlHookCtrl::Remove
},
}
},
None => {
event_send.send(LoopEvent::Remove(input_hook_id)).unwrap();
IntvlHookCtrl::Remove
},
}
});
self.parent.input.add_hook_with_id(
input_hook_id,
Hook {
target_id: self.parent.target.id(),
target_wk: self.parent.target.weak(),
state: HookState::Hold {
state,
pressed: false,
weight: self.weight,
intvl_id,
},
},
);
Ok(input_hook_id)
}
}
src/input/inner.rs (lines 55-124)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
pub(in crate::input) fn begin_loop(
interface: Arc<Interface>,
interval: Arc<Interval>,
event_send: Sender<LoopEvent>,
event_recv: Receiver<LoopEvent>,
) {
thread::spawn(move || {
let mut hooks: HashMap<InputHookID, Hook> = HashMap::new();
let mut win_state: HashMap<BstWindowID, WindowState> = HashMap::new();
let (ss_send, ss_recv) = channel::unbounded::<(BstWindowID, f32, f32)>();
struct SmoothScroll {
step: f32,
rem: [f32; 2],
amt: [f32; 2],
cycles: [u16; 2],
}
let mut ss_state: HashMap<BstWindowID, SmoothScroll> = HashMap::new();
const SS_CYCLES: u16 = 20;
// TODO: Configure frequency of output?
interval.start(interval.do_every(Duration::from_millis(8), None, move |_| {
while let Ok((win, v, h)) = ss_recv.try_recv() {
let mut state = ss_state.entry(win).or_insert_with(|| {
SmoothScroll {
step: 100.0,
rem: [0.0; 2],
amt: [0.0; 2],
cycles: [0; 2],
}
});
if v != 0.0 {
let accel = ((state.rem[0].abs() / state.step) / 1.5).clamp(1.0, 4.0);
state.rem[0] += v * state.step * accel;
state.amt[0] = state.rem[0];
state.cycles[0] = SS_CYCLES;
}
if h != 0.0 {
let accel = ((state.rem[1].abs() / state.step) / 1.5).clamp(1.0, 4.0);
state.rem[1] += h * state.step * accel;
state.amt[1] = state.rem[1];
state.cycles[1] = SS_CYCLES;
}
}
for (win, state) in ss_state.iter_mut() {
let v = if state.cycles[0] != 0 {
let amt =
state.amt[0] * ((state.cycles[0] as f32 - 0.5) / (SS_CYCLES as f32 * 10.0));
state.rem[0] -= amt;
state.cycles[0] -= 1;
if state.cycles[0] == 0 {
state.rem[0] = 0.0;
}
amt
} else {
0.0
};
let h = if state.cycles[1] != 0 {
let amt =
state.amt[1] * ((state.cycles[1] as f32 - 0.5) / (SS_CYCLES as f32 * 10.0));
state.rem[1] -= amt;
state.cycles[1] -= 1;
if state.cycles[1] == 0 {
state.rem[1] = 0.0;
}
amt
} else {
0.0
};
if v != 0.0 || h != 0.0 {
event_send
.send(LoopEvent::SmoothScroll {
win: *win,
v,
h,
})
.unwrap();
}
}
Default::default()
}));
while let Ok(event) = event_recv.recv() {
match event {
LoopEvent::Add {
id,
hook,
} => {
hooks.insert(id, hook);
},
LoopEvent::Remove(id) => {
hooks.remove(&id);
},
LoopEvent::FocusBin {
win,
bin,
} => {
let window_state = win_state
.entry(win)
.or_insert_with(|| WindowState::new(win));
if let Some((old_bin_id_op, new_bin_id_op)) = window_state.update_focus_bin(bin)
{
proc::bin_focus(
&interval,
&mut hooks,
window_state,
old_bin_id_op,
new_bin_id_op,
);
}
},
LoopEvent::SmoothScroll {
win,
v,
h,
} => {
proc::scroll(&interface, &mut hooks, &mut win_state, win, true, v, h);
},
LoopEvent::Normal(event) => {
match event {
InputEvent::Press {
win,
key,
} => {
proc::press(
&interface,
&interval,
&mut hooks,
&mut win_state,
win,
key,
);
},
InputEvent::Release {
win,
key,
} => {
proc::release(&interval, &mut hooks, &mut win_state, win, key);
},
InputEvent::Character {
win,
c,
} => {
proc::character(&mut hooks, &mut win_state, win, c);
},
InputEvent::Focus {
win,
} => {
proc::window_focus(&mut hooks, &mut win_state, win, true);
},
InputEvent::FocusLost {
win,
} => {
proc::window_focus(&mut hooks, &mut win_state, win, false);
},
InputEvent::Cursor {
win,
x,
y,
} => {
proc::cursor(&interface, &mut hooks, &mut win_state, win, x, y, false);
},
InputEvent::Scroll {
win,
v,
h,
} => {
ss_send.send((win, v, h)).unwrap();
proc::scroll(&interface, &mut hooks, &mut win_state, win, false, v, h);
},
InputEvent::Enter {
win,
} => {
proc::window_cursor_inside(&mut hooks, &mut win_state, win, true);
},
InputEvent::Leave {
win,
} => {
proc::window_cursor_inside(&mut hooks, &mut win_state, win, false);
},
InputEvent::Motion {
x,
y,
} => {
proc::motion(&mut hooks, x, y);
},
InputEvent::CursorCapture {
win,
captured,
} => {
let window_state = win_state
.entry(win)
.or_insert_with(|| WindowState::new(win));
if window_state.update_cursor_captured(captured) {
if captured {
if let Some((old_bin_id_op, ..)) =
window_state.update_focus_bin(None)
{
proc::bin_focus(
&interval,
&mut hooks,
window_state,
old_bin_id_op,
None,
);
}
}
let [x, y] = window_state.cursor_pos();
proc::cursor(
&interface,
&mut hooks,
&mut win_state,
win,
x,
y,
true,
);
}
},
}
},
}
}
});
}
sourcepub fn pause(&self, id: IntvlHookID)
pub fn pause(&self, id: IntvlHookID)
Examples found in repository?
src/input/proc/release.rs (line 59)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
pub(in crate::input) fn release(
interval: &Arc<Interval>,
hooks: &mut HashMap<InputHookID, Hook>,
win_state: &mut HashMap<BstWindowID, WindowState>,
win: BstWindowID,
key: Key,
) {
let window_state = win_state
.entry(win)
.or_insert_with(|| WindowState::new(win));
if window_state.update_key(key, false) {
let focused_bin_id = window_state.focused_bin_id();
let mut remove_hooks: Vec<InputHookID> = Vec::new();
let mut call_release_on: Vec<_> = hooks
.iter_mut()
.filter_map(|(hook_id, hook)| {
if hook.is_for_window_id(win)
|| (focused_bin_id.is_some() && hook.is_for_bin_id(focused_bin_id.unwrap()))
{
match &mut hook.state {
HookState::Release {
state,
pressed,
weight,
..
} => {
if state.is_involved(key) && !state.update(key, false) && *pressed {
*pressed = false;
Some((*weight, (hook_id, hook)))
} else {
None
}
},
HookState::Press {
state, ..
} => {
state.update(key, false);
None
},
HookState::Hold {
state,
pressed,
intvl_id,
..
} => {
if state.is_involved(key) && !state.update(key, false) && *pressed {
*pressed = false;
interval.pause(*intvl_id);
}
None
},
_ => None,
}
} else {
None
}
})
.collect();
call_release_on.sort_by_key(|(weight, _)| Reverse(*weight));
for (weight, (hook_id, hook)) in call_release_on {
let hook_target = match hook.target_wk.upgrade() {
Some(some) => some,
None => {
remove_hooks.push(*hook_id);
continue;
},
};
if let HookState::Release {
state,
method,
..
} = &mut hook.state
{
match method(hook_target, window_state, state) {
InputHookCtrl::Retain => (),
InputHookCtrl::RetainNoPass => {
if weight != NO_HOOK_WEIGHT {
break;
}
},
InputHookCtrl::Remove => {
remove_hooks.push(*hook_id);
},
InputHookCtrl::RemoveNoPass => {
remove_hooks.push(*hook_id);
if weight != NO_HOOK_WEIGHT {
break;
}
},
}
} else {
unreachable!()
}
}
}
}
More examples
src/input/proc/bin_focus.rs (line 41)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
pub(in crate::input) fn bin_focus(
interval: &Arc<Interval>,
hooks: &mut HashMap<InputHookID, Hook>,
window_state: &mut WindowState,
old_bin_id_op: Option<BinID>,
new_bin_id_op: Option<BinID>,
) {
let mut remove_hooks = Vec::new();
if let Some(old_bin_id) = old_bin_id_op {
let mut call_release_on = Vec::new();
let mut call_focus_lost_on = Vec::new();
for (hook_id, hook) in hooks.iter_mut() {
if hook.is_for_bin_id(old_bin_id) {
match &mut hook.state {
HookState::Release {
pressed,
weight,
..
} if *pressed => {
call_release_on.push((*weight, (hook_id, hook)));
},
HookState::Hold {
state,
pressed,
intvl_id,
..
} => {
if *pressed {
*pressed = false;
interval.pause(*intvl_id);
}
state.release_all();
},
HookState::FocusLost {
weight, ..
} => {
call_focus_lost_on.push((*weight, (hook_id, hook)));
},
_ => (),
}
}
}
call_release_on.sort_by_key(|(weight, _)| Reverse(*weight));
call_focus_lost_on.sort_by_key(|(weight, _)| Reverse(*weight));
let mut call_release_method = true;
for (weight, (hook_id, hook)) in call_release_on {
let hook_target = match hook.target_wk.upgrade() {
Some(some) => some,
None => {
remove_hooks.push(*hook_id);
continue;
},
};
if let HookState::Release {
state,
pressed,
method,
..
} = &mut hook.state
{
state.release_all();
*pressed = false;
if call_release_method {
match method(hook_target, window_state, state) {
InputHookCtrl::Retain => (),
InputHookCtrl::RetainNoPass => {
if weight != NO_HOOK_WEIGHT {
call_release_method = false;
}
},
InputHookCtrl::Remove => {
remove_hooks.push(*hook_id);
},
InputHookCtrl::RemoveNoPass => {
remove_hooks.push(*hook_id);
if weight != NO_HOOK_WEIGHT {
call_release_method = false;
}
},
}
}
} else {
unreachable!()
}
}
for (weight, (hook_id, hook)) in call_focus_lost_on {
let hook_target = match hook.target_wk.upgrade() {
Some(some) => some,
None => {
remove_hooks.push(*hook_id);
continue;
},
};
if let HookState::FocusLost {
method, ..
} = &mut hook.state
{
match method(hook_target, window_state) {
InputHookCtrl::Retain => (),
InputHookCtrl::RetainNoPass => {
if weight != NO_HOOK_WEIGHT {
break;
}
},
InputHookCtrl::Remove => {
remove_hooks.push(*hook_id);
},
InputHookCtrl::RemoveNoPass => {
remove_hooks.push(*hook_id);
if weight != NO_HOOK_WEIGHT {
break;
}
},
}
} else {
unreachable!()
}
}
}
if let Some(new_bin_id) = new_bin_id_op {
let mut call_focus_on: Vec<_> = hooks
.iter_mut()
.filter_map(|(hook_id, hook)| {
if hook.is_for_bin_id(new_bin_id) {
if let HookState::Focus {
weight, ..
} = &hook.state
{
Some((*weight, (hook_id, hook)))
} else {
None
}
} else {
None
}
})
.collect();
call_focus_on.sort_by_key(|(weight, _)| Reverse(*weight));
for (weight, (hook_id, hook)) in call_focus_on {
let hook_target = match hook.target_wk.upgrade() {
Some(some) => some,
None => {
remove_hooks.push(*hook_id);
continue;
},
};
if let HookState::Focus {
method, ..
} = &mut hook.state
{
match method(hook_target, window_state) {
InputHookCtrl::Retain => (),
InputHookCtrl::RetainNoPass => {
if weight != NO_HOOK_WEIGHT {
break;
}
},
InputHookCtrl::Remove => {
remove_hooks.push(*hook_id);
},
InputHookCtrl::RemoveNoPass => {
remove_hooks.push(*hook_id);
if weight != NO_HOOK_WEIGHT {
break;
}
},
}
}
}
}
for hook_id in remove_hooks {
hooks.remove(&hook_id);
}
}
sourcepub fn start(&self, id: IntvlHookID)
pub fn start(&self, id: IntvlHookID)
Examples found in repository?
src/input/inner.rs (lines 55-124)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
pub(in crate::input) fn begin_loop(
interface: Arc<Interface>,
interval: Arc<Interval>,
event_send: Sender<LoopEvent>,
event_recv: Receiver<LoopEvent>,
) {
thread::spawn(move || {
let mut hooks: HashMap<InputHookID, Hook> = HashMap::new();
let mut win_state: HashMap<BstWindowID, WindowState> = HashMap::new();
let (ss_send, ss_recv) = channel::unbounded::<(BstWindowID, f32, f32)>();
struct SmoothScroll {
step: f32,
rem: [f32; 2],
amt: [f32; 2],
cycles: [u16; 2],
}
let mut ss_state: HashMap<BstWindowID, SmoothScroll> = HashMap::new();
const SS_CYCLES: u16 = 20;
// TODO: Configure frequency of output?
interval.start(interval.do_every(Duration::from_millis(8), None, move |_| {
while let Ok((win, v, h)) = ss_recv.try_recv() {
let mut state = ss_state.entry(win).or_insert_with(|| {
SmoothScroll {
step: 100.0,
rem: [0.0; 2],
amt: [0.0; 2],
cycles: [0; 2],
}
});
if v != 0.0 {
let accel = ((state.rem[0].abs() / state.step) / 1.5).clamp(1.0, 4.0);
state.rem[0] += v * state.step * accel;
state.amt[0] = state.rem[0];
state.cycles[0] = SS_CYCLES;
}
if h != 0.0 {
let accel = ((state.rem[1].abs() / state.step) / 1.5).clamp(1.0, 4.0);
state.rem[1] += h * state.step * accel;
state.amt[1] = state.rem[1];
state.cycles[1] = SS_CYCLES;
}
}
for (win, state) in ss_state.iter_mut() {
let v = if state.cycles[0] != 0 {
let amt =
state.amt[0] * ((state.cycles[0] as f32 - 0.5) / (SS_CYCLES as f32 * 10.0));
state.rem[0] -= amt;
state.cycles[0] -= 1;
if state.cycles[0] == 0 {
state.rem[0] = 0.0;
}
amt
} else {
0.0
};
let h = if state.cycles[1] != 0 {
let amt =
state.amt[1] * ((state.cycles[1] as f32 - 0.5) / (SS_CYCLES as f32 * 10.0));
state.rem[1] -= amt;
state.cycles[1] -= 1;
if state.cycles[1] == 0 {
state.rem[1] = 0.0;
}
amt
} else {
0.0
};
if v != 0.0 || h != 0.0 {
event_send
.send(LoopEvent::SmoothScroll {
win: *win,
v,
h,
})
.unwrap();
}
}
Default::default()
}));
while let Ok(event) = event_recv.recv() {
match event {
LoopEvent::Add {
id,
hook,
} => {
hooks.insert(id, hook);
},
LoopEvent::Remove(id) => {
hooks.remove(&id);
},
LoopEvent::FocusBin {
win,
bin,
} => {
let window_state = win_state
.entry(win)
.or_insert_with(|| WindowState::new(win));
if let Some((old_bin_id_op, new_bin_id_op)) = window_state.update_focus_bin(bin)
{
proc::bin_focus(
&interval,
&mut hooks,
window_state,
old_bin_id_op,
new_bin_id_op,
);
}
},
LoopEvent::SmoothScroll {
win,
v,
h,
} => {
proc::scroll(&interface, &mut hooks, &mut win_state, win, true, v, h);
},
LoopEvent::Normal(event) => {
match event {
InputEvent::Press {
win,
key,
} => {
proc::press(
&interface,
&interval,
&mut hooks,
&mut win_state,
win,
key,
);
},
InputEvent::Release {
win,
key,
} => {
proc::release(&interval, &mut hooks, &mut win_state, win, key);
},
InputEvent::Character {
win,
c,
} => {
proc::character(&mut hooks, &mut win_state, win, c);
},
InputEvent::Focus {
win,
} => {
proc::window_focus(&mut hooks, &mut win_state, win, true);
},
InputEvent::FocusLost {
win,
} => {
proc::window_focus(&mut hooks, &mut win_state, win, false);
},
InputEvent::Cursor {
win,
x,
y,
} => {
proc::cursor(&interface, &mut hooks, &mut win_state, win, x, y, false);
},
InputEvent::Scroll {
win,
v,
h,
} => {
ss_send.send((win, v, h)).unwrap();
proc::scroll(&interface, &mut hooks, &mut win_state, win, false, v, h);
},
InputEvent::Enter {
win,
} => {
proc::window_cursor_inside(&mut hooks, &mut win_state, win, true);
},
InputEvent::Leave {
win,
} => {
proc::window_cursor_inside(&mut hooks, &mut win_state, win, false);
},
InputEvent::Motion {
x,
y,
} => {
proc::motion(&mut hooks, x, y);
},
InputEvent::CursorCapture {
win,
captured,
} => {
let window_state = win_state
.entry(win)
.or_insert_with(|| WindowState::new(win));
if window_state.update_cursor_captured(captured) {
if captured {
if let Some((old_bin_id_op, ..)) =
window_state.update_focus_bin(None)
{
proc::bin_focus(
&interval,
&mut hooks,
window_state,
old_bin_id_op,
None,
);
}
}
let [x, y] = window_state.cursor_pos();
proc::cursor(
&interface,
&mut hooks,
&mut win_state,
win,
x,
y,
true,
);
}
},
}
},
}
}
});
}
More examples
src/input/proc/press.rs (line 127)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
pub(in crate::input) fn press(
interface: &Arc<Interface>,
interval: &Arc<Interval>,
hooks: &mut HashMap<InputHookID, Hook>,
win_state: &mut HashMap<BstWindowID, WindowState>,
win: BstWindowID,
key: Key,
) {
let window_state = win_state
.entry(win)
.or_insert_with(|| WindowState::new(win));
// Returns true if the state changed
if window_state.update_key(key, true) {
let mut proc_in_order: Vec<_> = hooks
.iter_mut()
.filter_map(|(hook_id, hook)| {
if hook.is_for_window_id(win) {
match &mut hook.state {
HookState::Press {
state,
weight,
..
} => {
if state.update(key, true) {
Some((*weight, (hook_id, hook)))
} else {
None
}
},
HookState::Release {
state,
weight,
..
} => {
if state.is_involved(key) {
Some((*weight, (hook_id, hook)))
} else {
None
}
},
HookState::Hold {
state,
weight,
..
} => {
if state.is_involved(key) {
Some((*weight, (hook_id, hook)))
} else {
None
}
},
_ => None,
}
} else {
None
}
})
.collect();
proc_in_order.sort_by_key(|(weight, _)| Reverse(*weight));
let mut pass_bin_event = true;
let mut remove_hooks: Vec<InputHookID> = Vec::new();
for (weight, (hook_id, hook)) in proc_in_order {
match &mut hook.state {
HookState::Press {
state,
method,
..
} => {
match hook.target_wk.upgrade() {
Some(hook_target) => {
match method(hook_target, window_state, state) {
InputHookCtrl::Retain => (),
InputHookCtrl::RetainNoPass => {
if weight != NO_HOOK_WEIGHT {
pass_bin_event = false;
break;
}
},
InputHookCtrl::Remove => {
remove_hooks.push(*hook_id);
},
InputHookCtrl::RemoveNoPass => {
remove_hooks.push(*hook_id);
if weight != NO_HOOK_WEIGHT {
pass_bin_event = false;
break;
}
},
}
},
None => {
remove_hooks.push(*hook_id);
},
}
},
HookState::Release {
state,
pressed,
..
} => {
if state.update(key, true) {
*pressed = true;
}
},
HookState::Hold {
state,
pressed,
intvl_id,
..
} => {
if state.update(key, true) {
*pressed = true;
interval.start(*intvl_id);
}
},
_ => unreachable!(),
}
}
if pass_bin_event && !window_state.is_cursor_captured() {
// Check Bin Focus
if key == BIN_FOCUS_KEY {
if let Some((old_bin_id_op, new_bin_id_op)) =
window_state.check_focus_bin(interface)
{
proc::bin_focus(interval, hooks, window_state, old_bin_id_op, new_bin_id_op);
}
}
if let Some(focus_bin_id) = window_state.focused_bin_id() {
let mut call_in_order: Vec<_> = hooks
.iter_mut()
.filter_map(|(hook_id, hook)| {
if hook.is_for_bin_id(focus_bin_id) {
match &mut hook.state {
HookState::Press {
state,
weight,
..
} => {
if state.update(key, true) {
Some((*weight, (hook_id, hook)))
} else {
None
}
},
HookState::Release {
state,
weight,
..
} => {
if state.is_involved(key) {
Some((*weight, (hook_id, hook)))
} else {
None
}
},
HookState::Hold {
state,
weight,
..
} => {
if state.is_involved(key) {
Some((*weight, (hook_id, hook)))
} else {
None
}
},
_ => None,
}
} else {
None
}
})
.collect();
call_in_order.sort_by_key(|(weight, _)| Reverse(*weight));
for (weight, (hook_id, hook)) in call_in_order {
match &mut hook.state {
HookState::Press {
state,
method,
..
} => {
match hook.target_wk.upgrade() {
Some(hook_target) => {
match method(hook_target, window_state, state) {
InputHookCtrl::Retain => (),
InputHookCtrl::RetainNoPass => {
if weight != NO_HOOK_WEIGHT {
break;
}
},
InputHookCtrl::Remove => {
remove_hooks.push(*hook_id);
},
InputHookCtrl::RemoveNoPass => {
remove_hooks.push(*hook_id);
if weight != NO_HOOK_WEIGHT {
break;
}
},
}
},
None => {
remove_hooks.push(*hook_id);
},
}
},
HookState::Release {
state,
pressed,
..
} => {
if state.update(key, true) {
*pressed = true;
}
},
HookState::Hold {
state,
pressed,
intvl_id,
..
} => {
if state.update(key, true) {
*pressed = true;
interval.start(*intvl_id);
}
},
_ => unreachable!(),
}
}
}
}
for hook_id in remove_hooks {
hooks.remove(&hook_id);
}
}
}