A card's sliders follow the hand, not only the press
Browse files at this commit2ed6fe78f87560178df4ea2beef627d9f3e7b120Parent: dc279aa- fix
- a card's sliders follow the hand, not only the press
2 changed files +155 -118
- crates/app/src/main.rs +152 -115
- crates/app/src/ui/cards.rs +3 -3
crates/app/src/main.rsmodified+152 -115
| @@ -550,6 +550,25 @@ fn scrim() -> Div { | |||
| 550 | div().absolute().inset_0().occlude() | 550 | div().absolute().inset_0().occlude() |
| 551 | } | 551 | } |
| 552 | 552 | ||
| 553 | +/// The sheet a card with a *slider* in it takes instead: the same scrim, with | ||
| 554 | +/// the window's own drag listeners on it as well. | ||
| 555 | +/// | ||
| 556 | +/// A scrim occludes, and occluding is where gpui's hit test stops | ||
| 557 | +/// (`Hitbox::is_hovered`, window.rs:788) -- so while a card is up the root is | ||
| 558 | +/// not hovered anywhere behind it and its `on_mouse_move`/`on_mouse_up` hear | ||
| 559 | +/// nothing at all. Every drag in this window is tracked from the root, because | ||
| 560 | +/// each of them starts on a strip a few pixels wide that the pointer leaves at | ||
| 561 | +/// once ([`Player::drag_move`]), so a card's handles were set by the press and | ||
| 562 | +/// then frozen: the value never followed the hand and the release never wrote. | ||
| 563 | +/// The scrim is the one surface above the occluder that covers the whole card, | ||
| 564 | +/// so the same two listeners go here, and a drag that leaves the card is picked | ||
| 565 | +/// up by the root's copy of them without a seam. | ||
| 566 | +fn drag_scrim(cx: &mut Context<Player>) -> Div { | ||
| 567 | + scrim() | ||
| 568 | + .on_mouse_move(cx.listener(Player::drag_move)) | ||
| 569 | + .on_mouse_up(MouseButton::Left, cx.listener(Player::drag_release)) | ||
| 570 | +} | ||
| 571 | + | ||
| 553 | /// A press that stops here. What every card's body hands its scrim: the scrim | 572 | /// A press that stops here. What every card's body hands its scrim: the scrim |
| 554 | /// closes the card on a press, and the card is painted after it, so this listener | 573 | /// closes the card on a press, and the card is painted after it, so this listener |
| 555 | /// runs first (gpui dispatches topmost-first, window.rs:3705) and a press meant | 574 | /// runs first (gpui dispatches topmost-first, window.rs:3705) and a press meant |
| @@ -5097,6 +5116,137 @@ impl Player { | |||
| 5097 | self.set_volume(|volume| volume.set_along(along), cx); | 5116 | self.set_volume(|volume| volume.set_along(along), cx); |
| 5098 | } | 5117 | } |
| 5099 | 5118 | ||
| 5119 | + /// One sample of whatever drag is in the hand: the equalizer's handle, a | ||
| 5120 | + /// clip's edge, a colour bar, the speed bar, the volume slider or the | ||
| 5121 | + /// playhead. Each of those starts on a strip a few pixels wide that the | ||
| 5122 | + /// pointer leaves immediately, so none of them can be tracked from the | ||
| 5123 | + /// element it started on -- the gesture is followed here instead, on a | ||
| 5124 | + /// hitbox that covers everything the hand can reach. | ||
| 5125 | + /// | ||
| 5126 | + /// Registered on the root *and* on the scrim of every card that holds a | ||
| 5127 | + /// slider ([`Player::drag_scrim`]). An occluding sheet ends gpui's hit test | ||
| 5128 | + /// where it sits (`Hitbox::is_hovered`, window.rs:788), so while a card is | ||
| 5129 | + /// up the root is not hovered anywhere under it and hears none of this: the | ||
| 5130 | + /// press set a value and the drag then froze on it. | ||
| 5131 | + fn drag_move(&mut self, event: &MouseMoveEvent, _: &mut Window, cx: &mut Context<Self>) { | ||
| 5132 | + // A handle is 10 px across and the pointer leaves it at once, so | ||
| 5133 | + // the equalizer drag is tracked here for the ruler's reason. | ||
| 5134 | + if self.eq_dragging { | ||
| 5135 | + if event.pressed_button == Some(MouseButton::Left) { | ||
| 5136 | + self.drag_band(event.position, cx); | ||
| 5137 | + } else { | ||
| 5138 | + // Released outside the window: the up below never came, | ||
| 5139 | + // so this is where the gesture ends -- and it still owes | ||
| 5140 | + // the one write the whole drag is worth. | ||
| 5141 | + self.eq_dragging = false; | ||
| 5142 | + self.commit_eq(cx); | ||
| 5143 | + } | ||
| 5144 | + return; | ||
| 5145 | + } | ||
| 5146 | + // A clip edge is 6 px wide and the pointer leaves it on the | ||
| 5147 | + // first drag, so the gesture is tracked here for the same | ||
| 5148 | + // reason -- and it ends here too when the button came up | ||
| 5149 | + // outside the window, still owing its one edit. | ||
| 5150 | + if self.trim.is_some() { | ||
| 5151 | + match event.pressed_button { | ||
| 5152 | + Some(MouseButton::Left) => self.trim_to(event.position.x, cx), | ||
| 5153 | + _ => self.commit_trim(cx), | ||
| 5154 | + } | ||
| 5155 | + return; | ||
| 5156 | + } | ||
| 5157 | + // A colour slider is 4 px tall and the pointer leaves it just as | ||
| 5158 | + // fast; every sample is live, so the release owes no write of | ||
| 5159 | + // its own -- what the last sample set is what the clip carries. | ||
| 5160 | + if self.color_dragging { | ||
| 5161 | + if event.pressed_button == Some(MouseButton::Left) { | ||
| 5162 | + self.drag_color(event.position.x, false, cx); | ||
| 5163 | + } else { | ||
| 5164 | + // The release happened outside the window, so this is | ||
| 5165 | + // where the gesture ends -- and it may not end on a | ||
| 5166 | + // sample the worker was too busy to take. | ||
| 5167 | + self.color_dragging = false; | ||
| 5168 | + self.flush_drag(cx); | ||
| 5169 | + } | ||
| 5170 | + return; | ||
| 5171 | + } | ||
| 5172 | + // The speed bar, the same 4 px and the same live writes: the | ||
| 5173 | + // press took the undo step and every sample since is live. | ||
| 5174 | + if self.speed_dragging { | ||
| 5175 | + if event.pressed_button == Some(MouseButton::Left) { | ||
| 5176 | + self.drag_speed(event.position.x, false, cx); | ||
| 5177 | + } else { | ||
| 5178 | + self.speed_dragging = false; | ||
| 5179 | + self.flush_drag(cx); | ||
| 5180 | + } | ||
| 5181 | + return; | ||
| 5182 | + } | ||
| 5183 | + // The volume slider, the same live writes: what the hand is on | ||
| 5184 | + // is what the speakers are doing, and there is nothing to undo. | ||
| 5185 | + if self.volume_dragging { | ||
| 5186 | + if event.pressed_button == Some(MouseButton::Left) { | ||
| 5187 | + self.drag_volume(event.position.x, cx); | ||
| 5188 | + } else { | ||
| 5189 | + self.volume_dragging = false; | ||
| 5190 | + } | ||
| 5191 | + return; | ||
| 5192 | + } | ||
| 5193 | + if !self.scrubbing { | ||
| 5194 | + return; | ||
| 5195 | + } | ||
| 5196 | + if event.pressed_button == Some(MouseButton::Left) { | ||
| 5197 | + self.scrub_to(event.position.x, false, cx); | ||
| 5198 | + } else { | ||
| 5199 | + // A release outside the window never reaches the handler | ||
| 5200 | + // below, so the first button-up move is when we learn the | ||
| 5201 | + // drag is over. Without this the next hover would scrub. | ||
| 5202 | + self.scrubbing = false; | ||
| 5203 | + } | ||
| 5204 | + } | ||
| 5205 | + | ||
| 5206 | + /// Where a drag ends: the release lands exactly, and whatever the gesture | ||
| 5207 | + /// owes -- one undo step for the equalizer and the trim, a flush for the | ||
| 5208 | + /// live-writing bars -- is paid here. On the root and on a card's scrim | ||
| 5209 | + /// both, for [`Player::drag_move`]'s reason: a release over an open card | ||
| 5210 | + /// never reaches the root. | ||
| 5211 | + fn drag_release(&mut self, event: &MouseUpEvent, _: &mut Window, cx: &mut Context<Self>) { | ||
| 5212 | + if std::mem::take(&mut self.eq_dragging) { | ||
| 5213 | + // The release lands exactly, then the gesture is written | ||
| 5214 | + // once -- the append-only table's whole reason. | ||
| 5215 | + self.drag_band(event.position, cx); | ||
| 5216 | + self.commit_eq(cx); | ||
| 5217 | + return; | ||
| 5218 | + } | ||
| 5219 | + if self.trim.is_some() { | ||
| 5220 | + // The release lands exactly, then the gesture is | ||
| 5221 | + // written once -- one edit, one undo step. | ||
| 5222 | + self.trim_to(event.position.x, cx); | ||
| 5223 | + self.commit_trim(cx); | ||
| 5224 | + return; | ||
| 5225 | + } | ||
| 5226 | + if std::mem::take(&mut self.color_dragging) { | ||
| 5227 | + // The release lands exactly where the hand let go, and | ||
| 5228 | + // it is a live write like every other sample: the undo | ||
| 5229 | + // step the gesture rolls back to was the press's. The | ||
| 5230 | + // flush is what makes "exactly" true while the worker is | ||
| 5231 | + // still busy -- the sample above would only be held. | ||
| 5232 | + self.drag_color(event.position.x, false, cx); | ||
| 5233 | + self.flush_drag(cx); | ||
| 5234 | + return; | ||
| 5235 | + } | ||
| 5236 | + if std::mem::take(&mut self.speed_dragging) { | ||
| 5237 | + self.drag_speed(event.position.x, false, cx); | ||
| 5238 | + self.flush_drag(cx); | ||
| 5239 | + return; | ||
| 5240 | + } | ||
| 5241 | + if std::mem::take(&mut self.volume_dragging) { | ||
| 5242 | + self.drag_volume(event.position.x, cx); | ||
| 5243 | + return; | ||
| 5244 | + } | ||
| 5245 | + if std::mem::take(&mut self.scrubbing) { | ||
| 5246 | + self.scrub_to(event.position.x, true, cx); | ||
| 5247 | + } | ||
| 5248 | + } | ||
| 5249 | + | ||
| 5100 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { | 5250 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { |
| 5101 | if self.exporting().is_some() { | 5251 | if self.exporting().is_some() { |
| 5102 | return; | 5252 | return; |
| @@ -5894,121 +6044,8 @@ impl Render for Player { | |||
| 5894 | // Scrubbing is tracked on the root because the pointer leaves the | 6044 | // Scrubbing is tracked on the root because the pointer leaves the |
| 5895 | // 6 px ruler on the first drag and its own listeners then stop | 6045 | // 6 px ruler on the first drag and its own listeners then stop |
| 5896 | // firing; the root's hitbox is the whole window. | 6046 | // firing; the root's hitbox is the whole window. |
| 5897 | - .on_mouse_move(cx.listener(|this, event: &MouseMoveEvent, _, cx| { | 6047 | + .on_mouse_move(cx.listener(Self::drag_move)) |
| 5898 | - // A handle is 10 px across and the pointer leaves it at once, so | 6048 | + .on_mouse_up(MouseButton::Left, cx.listener(Self::drag_release)) |
| 5899 | - // the equalizer drag is tracked here for the ruler's reason. | ||
| 5900 | - if this.eq_dragging { | ||
| 5901 | - if event.pressed_button == Some(MouseButton::Left) { | ||
| 5902 | - this.drag_band(event.position, cx); | ||
| 5903 | - } else { | ||
| 5904 | - // Released outside the window: the up below never came, | ||
| 5905 | - // so this is where the gesture ends -- and it still owes | ||
| 5906 | - // the one write the whole drag is worth. | ||
| 5907 | - this.eq_dragging = false; | ||
| 5908 | - this.commit_eq(cx); | ||
| 5909 | - } | ||
| 5910 | - return; | ||
| 5911 | - } | ||
| 5912 | - // A clip edge is 6 px wide and the pointer leaves it on the | ||
| 5913 | - // first drag, so the gesture is tracked here for the same | ||
| 5914 | - // reason -- and it ends here too when the button came up | ||
| 5915 | - // outside the window, still owing its one edit. | ||
| 5916 | - if this.trim.is_some() { | ||
| 5917 | - match event.pressed_button { | ||
| 5918 | - Some(MouseButton::Left) => this.trim_to(event.position.x, cx), | ||
| 5919 | - _ => this.commit_trim(cx), | ||
| 5920 | - } | ||
| 5921 | - return; | ||
| 5922 | - } | ||
| 5923 | - // A colour slider is 4 px tall and the pointer leaves it just as | ||
| 5924 | - // fast; every sample is live, so the release owes no write of | ||
| 5925 | - // its own -- what the last sample set is what the clip carries. | ||
| 5926 | - if this.color_dragging { | ||
| 5927 | - if event.pressed_button == Some(MouseButton::Left) { | ||
| 5928 | - this.drag_color(event.position.x, false, cx); | ||
| 5929 | - } else { | ||
| 5930 | - // The release happened outside the window, so this is | ||
| 5931 | - // where the gesture ends -- and it may not end on a | ||
| 5932 | - // sample the worker was too busy to take. | ||
| 5933 | - this.color_dragging = false; | ||
| 5934 | - this.flush_drag(cx); | ||
| 5935 | - } | ||
| 5936 | - return; | ||
| 5937 | - } | ||
| 5938 | - // The speed bar, the same 4 px and the same live writes: the | ||
| 5939 | - // press took the undo step and every sample since is live. | ||
| 5940 | - if this.speed_dragging { | ||
| 5941 | - if event.pressed_button == Some(MouseButton::Left) { | ||
| 5942 | - this.drag_speed(event.position.x, false, cx); | ||
| 5943 | - } else { | ||
| 5944 | - this.speed_dragging = false; | ||
| 5945 | - this.flush_drag(cx); | ||
| 5946 | - } | ||
| 5947 | - return; | ||
| 5948 | - } | ||
| 5949 | - // The volume slider, the same live writes: what the hand is on | ||
| 5950 | - // is what the speakers are doing, and there is nothing to undo. | ||
| 5951 | - if this.volume_dragging { | ||
| 5952 | - if event.pressed_button == Some(MouseButton::Left) { | ||
| 5953 | - this.drag_volume(event.position.x, cx); | ||
| 5954 | - } else { | ||
| 5955 | - this.volume_dragging = false; | ||
| 5956 | - } | ||
| 5957 | - return; | ||
| 5958 | - } | ||
| 5959 | - if !this.scrubbing { | ||
| 5960 | - return; | ||
| 5961 | - } | ||
| 5962 | - if event.pressed_button == Some(MouseButton::Left) { | ||
| 5963 | - this.scrub_to(event.position.x, false, cx); | ||
| 5964 | - } else { | ||
| 5965 | - // A release outside the window never reaches the handler | ||
| 5966 | - // below, so the first button-up move is when we learn the | ||
| 5967 | - // drag is over. Without this the next hover would scrub. | ||
| 5968 | - this.scrubbing = false; | ||
| 5969 | - } | ||
| 5970 | - })) | ||
| 5971 | - .on_mouse_up( | ||
| 5972 | - MouseButton::Left, | ||
| 5973 | - cx.listener(|this, event: &MouseUpEvent, _, cx| { | ||
| 5974 | - if std::mem::take(&mut this.eq_dragging) { | ||
| 5975 | - // The release lands exactly, then the gesture is written | ||
| 5976 | - // once -- the append-only table's whole reason. | ||
| 5977 | - this.drag_band(event.position, cx); | ||
| 5978 | - this.commit_eq(cx); | ||
| 5979 | - return; | ||
| 5980 | - } | ||
| 5981 | - if this.trim.is_some() { | ||
| 5982 | - // The release lands exactly, then the gesture is | ||
| 5983 | - // written once -- one edit, one undo step. | ||
| 5984 | - this.trim_to(event.position.x, cx); | ||
| 5985 | - this.commit_trim(cx); | ||
| 5986 | - return; | ||
| 5987 | - } | ||
| 5988 | - if std::mem::take(&mut this.color_dragging) { | ||
| 5989 | - // The release lands exactly where the hand let go, and | ||
| 5990 | - // it is a live write like every other sample: the undo | ||
| 5991 | - // step the gesture rolls back to was the press's. The | ||
| 5992 | - // flush is what makes "exactly" true while the worker is | ||
| 5993 | - // still busy -- the sample above would only be held. | ||
| 5994 | - this.drag_color(event.position.x, false, cx); | ||
| 5995 | - this.flush_drag(cx); | ||
| 5996 | - return; | ||
| 5997 | - } | ||
| 5998 | - if std::mem::take(&mut this.speed_dragging) { | ||
| 5999 | - this.drag_speed(event.position.x, false, cx); | ||
| 6000 | - this.flush_drag(cx); | ||
| 6001 | - return; | ||
| 6002 | - } | ||
| 6003 | - if std::mem::take(&mut this.volume_dragging) { | ||
| 6004 | - this.drag_volume(event.position.x, cx); | ||
| 6005 | - return; | ||
| 6006 | - } | ||
| 6007 | - if std::mem::take(&mut this.scrubbing) { | ||
| 6008 | - this.scrub_to(event.position.x, true, cx); | ||
| 6009 | - } | ||
| 6010 | - }), | ||
| 6011 | - ) | ||
| 6012 | .size_full() | 6049 | .size_full() |
| 6013 | .flex() | 6050 | .flex() |
| 6014 | .flex_col() | 6051 | .flex_col() |
crates/app/src/ui/cards.rsmodified+3 -3
| @@ -747,7 +747,7 @@ impl Player { | |||
| 747 | // corner as the 20 Hz tick, and the two lines above it (+6 and -6) | 747 | // corner as the 20 Hz tick, and the two lines above it (+6 and -6) |
| 748 | // already say what the box is worth per pixel. | 748 | // already say what the box is worth per pixel. |
| 749 | Some( | 749 | Some( |
| 750 | - scrim() | 750 | + drag_scrim(cx) |
| 751 | .flex() | 751 | .flex() |
| 752 | .justify_center() | 752 | .justify_center() |
| 753 | .items_center() | 753 | .items_center() |
| @@ -1085,7 +1085,7 @@ impl Player { | |||
| 1085 | }) | 1085 | }) |
| 1086 | .collect(); | 1086 | .collect(); |
| 1087 | Some( | 1087 | Some( |
| 1088 | - scrim() | 1088 | + drag_scrim(cx) |
| 1089 | .flex() | 1089 | .flex() |
| 1090 | .justify_center() | 1090 | .justify_center() |
| 1091 | .items_center() | 1091 | .items_center() |
| @@ -1202,7 +1202,7 @@ impl Player { | |||
| 1202 | }) | 1202 | }) |
| 1203 | .collect(); | 1203 | .collect(); |
| 1204 | Some( | 1204 | Some( |
| 1205 | - scrim() | 1205 | + drag_scrim(cx) |
| 1206 | .flex() | 1206 | .flex() |
| 1207 | .justify_center() | 1207 | .justify_center() |
| 1208 | .items_center() | 1208 | .items_center() |