11use iced:: {
2+ advanced:: {
3+ layout,
4+ renderer:: { self } ,
5+ widget:: { self , Operation } ,
6+ Clipboard , Layout , Shell , Widget ,
7+ } ,
28 event, mouse, Background , Color , Element , Event , Length , Point , Rectangle , Size , Vector ,
39} ;
4- use iced_native:: {
5- layout,
6- renderer:: { self } ,
7- widget:: { self , Operation } ,
8- Clipboard , Layout , Shell , Widget ,
9- } ;
1010
1111use crate :: {
1212 matrix:: Matrix ,
2121{
2222 width : Length ,
2323 height : Length ,
24- max_width : u32 ,
25- max_height : u32 ,
24+ max_width : f32 ,
25+ max_height : f32 ,
2626 style : <Renderer :: Theme as StyleSheet >:: Style ,
2727 content : Vec < GraphNodeElement < ' a , Message , Renderer > > ,
2828 matrix : Matrix ,
@@ -39,16 +39,15 @@ where
3939 Renderer : renderer:: Renderer ,
4040 Renderer :: Theme : StyleSheet ,
4141{
42- pub fn new ( content : Vec < GraphNodeElement < ' a , Message , Renderer > > ) -> Self
43- {
42+ pub fn new ( content : Vec < GraphNodeElement < ' a , Message , Renderer > > ) -> Self {
4443 GraphContainer {
4544 on_translate : None ,
4645 on_scale : None ,
4746 matrix : Matrix :: identity ( ) ,
4847 width : Length :: Shrink ,
4948 height : Length :: Shrink ,
50- max_width : u32 :: MAX ,
51- max_height : u32 :: MAX ,
49+ max_width : f32 :: MAX ,
50+ max_height : f32 :: MAX ,
5251 style : Default :: default ( ) ,
5352 content,
5453 }
@@ -85,12 +84,12 @@ where
8584 self
8685 }
8786
88- pub fn max_width ( mut self , max_width : u32 ) -> Self {
87+ pub fn max_width ( mut self , max_width : f32 ) -> Self {
8988 self . max_width = max_width;
9089 self
9190 }
9291
93- pub fn max_height ( mut self , max_height : u32 ) -> Self {
92+ pub fn max_height ( mut self , max_height : f32 ) -> Self {
9493 self . max_height = max_height;
9594 self
9695 }
@@ -138,6 +137,10 @@ where
138137 self . height
139138 }
140139
140+ fn tag ( & self ) -> widget:: tree:: Tag {
141+ widget:: tree:: Tag :: of :: < widget:: tree:: State > ( )
142+ }
143+
141144 fn state ( & self ) -> widget:: tree:: State {
142145 widget:: tree:: State :: new ( GraphContainerState {
143146 drag_start_position : None ,
@@ -176,7 +179,7 @@ where
176179 renderer : & Renderer ,
177180 operation : & mut dyn Operation < Message > ,
178181 ) {
179- operation. container ( None , & mut |operation| {
182+ operation. container ( None , layout . bounds ( ) , & mut |operation| {
180183 self . content
181184 . iter ( )
182185 . zip ( & mut tree. children )
@@ -194,29 +197,32 @@ where
194197 tree : & mut widget:: Tree ,
195198 event : Event ,
196199 layout : Layout < ' _ > ,
197- cursor_position : Point ,
200+ cursor : mouse :: Cursor ,
198201 renderer : & Renderer ,
199202 clipboard : & mut dyn Clipboard ,
200203 shell : & mut Shell < ' _ , Message > ,
204+ viewport : & Rectangle < f32 > ,
201205 ) -> event:: Status {
202206 let mut status = event:: Status :: Ignored ;
203207 let mut state = tree. state . downcast_mut :: < GraphContainerState > ( ) ;
204208
205209 if let Some ( start) = state. drag_start_position {
206- match event {
207- Event :: Mouse ( mouse:: Event :: ButtonReleased ( mouse:: Button :: Left ) ) => {
208- state. drag_start_position = None ;
209- }
210- Event :: Mouse ( mouse:: Event :: CursorMoved { .. } ) => {
211- let delta = cursor_position - start;
212- state. drag_start_position = Some ( cursor_position) ;
213- if let Some ( f) = & self . on_translate {
214- let message = f ( ( delta. x , delta. y ) ) ;
215- shell. publish ( message) ;
210+ if let Some ( cursor_position) = cursor. position ( ) {
211+ match event {
212+ Event :: Mouse ( mouse:: Event :: ButtonReleased ( mouse:: Button :: Left ) ) => {
213+ state. drag_start_position = None ;
214+ }
215+ Event :: Mouse ( mouse:: Event :: CursorMoved { .. } ) => {
216+ let delta = cursor_position - start;
217+ state. drag_start_position = Some ( cursor_position) ;
218+ if let Some ( f) = & self . on_translate {
219+ let message = f ( ( delta. x , delta. y ) ) ;
220+ shell. publish ( message) ;
221+ }
222+ status = event:: Status :: Captured ;
216223 }
217- status = event :: Status :: Captured ;
224+ _ => { }
218225 }
219- _ => { }
220226 }
221227 } else {
222228 status = self
@@ -229,37 +235,40 @@ where
229235 state,
230236 event. clone ( ) ,
231237 layout,
232- cursor_position ,
238+ cursor ,
233239 renderer,
234240 clipboard,
235241 shell,
242+ viewport,
236243 )
237244 } )
238245 . fold ( event:: Status :: Ignored , event:: Status :: merge) ;
239246 }
240247
241248 if status == event:: Status :: Ignored {
242- match event {
243- Event :: Mouse ( mouse:: Event :: ButtonPressed ( mouse:: Button :: Left ) ) => {
244- state. drag_start_position = Some ( cursor_position) ;
245- status = event:: Status :: Captured ;
246- }
247- Event :: Mouse ( mouse:: Event :: WheelScrolled { delta } ) => {
248- if let Some ( f) = & self . on_scale {
249- match delta {
250- mouse:: ScrollDelta :: Lines { y, .. } => {
251- let message = f ( cursor_position. x , cursor_position. y , y) ;
252- shell. publish ( message) ;
253- }
254- mouse:: ScrollDelta :: Pixels { y, .. } => {
255- let message = f ( cursor_position. x , cursor_position. y , y) ;
256- shell. publish ( message) ;
249+ if let Some ( cursor_position) = cursor. position ( ) {
250+ match event {
251+ Event :: Mouse ( mouse:: Event :: ButtonPressed ( mouse:: Button :: Left ) ) => {
252+ state. drag_start_position = Some ( cursor_position) ;
253+ status = event:: Status :: Captured ;
254+ }
255+ Event :: Mouse ( mouse:: Event :: WheelScrolled { delta } ) => {
256+ if let Some ( f) = & self . on_scale {
257+ match delta {
258+ mouse:: ScrollDelta :: Lines { y, .. } => {
259+ let message = f ( cursor_position. x , cursor_position. y , y) ;
260+ shell. publish ( message) ;
261+ }
262+ mouse:: ScrollDelta :: Pixels { y, .. } => {
263+ let message = f ( cursor_position. x , cursor_position. y , y) ;
264+ shell. publish ( message) ;
265+ }
257266 }
267+ status = event:: Status :: Captured ;
258268 }
259- status = event:: Status :: Captured ;
260269 }
270+ _ => { }
261271 }
262- _ => { }
263272 }
264273 }
265274
@@ -270,7 +279,7 @@ where
270279 & self ,
271280 tree : & widget:: Tree ,
272281 layout : Layout < ' _ > ,
273- cursor_position : Point ,
282+ cursor : mouse :: Cursor ,
274283 viewport : & Rectangle ,
275284 renderer : & Renderer ,
276285 ) -> mouse:: Interaction {
@@ -279,13 +288,9 @@ where
279288 . zip ( & tree. children )
280289 . zip ( layout. children ( ) )
281290 . map ( |( ( child, state) , layout) | {
282- child. as_widget ( ) . mouse_interaction (
283- state,
284- layout,
285- cursor_position,
286- viewport,
287- renderer,
288- )
291+ child
292+ . as_widget ( )
293+ . mouse_interaction ( state, layout, cursor, viewport, renderer)
289294 } )
290295 . max ( )
291296 . unwrap_or_default ( )
@@ -298,7 +303,7 @@ where
298303 theme : & Renderer :: Theme ,
299304 renderer_style : & renderer:: Style ,
300305 layout : Layout < ' _ > ,
301- cursor_position : Point ,
306+ cursor : mouse :: Cursor ,
302307 viewport : & Rectangle ,
303308 ) {
304309 let style = theme. appearance ( & self . style ) ;
@@ -372,7 +377,7 @@ where
372377 theme,
373378 renderer_style,
374379 layout,
375- cursor_position ,
380+ cursor ,
376381 & viewport,
377382 ) ;
378383 }
0 commit comments