22//
33// SPDX-License-Identifier: GPL-3.0-or-later
44
5- use crate :: dialogs:: { alert, ConfirmDialog , YES } ;
6- use crate :: git:: { commit, stash } ;
5+ use crate :: dialogs:: alert;
6+ use crate :: git:: commit;
77use crate :: status_view:: context:: StatusRenderContext ;
88use crate :: status_view:: {
99 render:: ViewContainer , stage_view:: StageView , view:: View , CursorPosition ,
1010 Label as TextViewLabel ,
1111} ;
12- use crate :: { CurrentWindow , Event } ;
12+ use crate :: { ApplyOp , CurrentWindow , Event , StageOp } ;
1313use async_channel:: Sender ;
1414use git2:: Oid ;
1515
1616use gtk4:: prelude:: * ;
1717use gtk4:: {
18- gdk, gio, glib, Button , EventControllerKey , Label , ScrolledWindow , TextBuffer , TextIter , Widget ,
18+ gdk, gio, glib, Button , EventControllerKey , Label , ScrolledWindow , TextBuffer , TextIter ,
1919} ;
2020use libadwaita:: prelude:: * ;
2121use libadwaita:: { HeaderBar , ToolbarView , Window } ;
2222use log:: { debug, info, trace} ;
2323
2424use std:: path:: PathBuf ;
2525
26- async fn git_oid_op < F > ( dialog : ConfirmDialog , window : impl IsA < Widget > , op : F )
27- where
28- F : FnOnce ( ) -> Result < ( ) , git2:: Error > + Send + ' static ,
29- {
30- let response = alert ( dialog) . choose_future ( & window) . await ;
31- if response != YES {
32- return ;
33- }
34- gio:: spawn_blocking ( op)
35- . await
36- . unwrap_or_else ( |e| {
37- alert ( format ! ( "{:?}" , e) ) . present ( Some ( & window) ) ;
38- Ok ( ( ) )
39- } )
40- . unwrap_or_else ( |e| {
41- alert ( e) . present ( Some ( & window) ) ;
42- } ) ;
43- }
44-
45- pub fn headerbar_factory (
46- repo_path : PathBuf ,
47- window : & impl IsA < Widget > ,
48- sender : Sender < Event > ,
49- oid : Oid ,
50- stash_num : Option < usize > ,
51- ) -> HeaderBar {
26+ pub fn headerbar_factory ( sender : Sender < Event > , oid : Oid , stash_num : Option < usize > ) -> HeaderBar {
5227 let hb = HeaderBar :: builder ( ) . build ( ) ;
5328 let ( btn_tooltip, title) = if stash_num. is_some ( ) {
5429 ( "Apply stash" , "Stash" )
@@ -70,25 +45,16 @@ pub fn headerbar_factory(
7045
7146 cherry_pick_btn. connect_clicked ( {
7247 let sender = sender. clone ( ) ;
73- let path = repo_path. clone ( ) ;
74- let window = window. clone ( ) ;
7548 move |_| {
7649 let sender = sender. clone ( ) ;
77- let path = path. clone ( ) ;
78- let window = window. clone ( ) ;
79- if let Some ( num) = stash_num {
80- glib:: spawn_future_local ( {
81- git_oid_op (
82- ConfirmDialog ( "Apply stash?" . to_string ( ) , format ! ( "{}" , num) ) ,
83- window,
84- move || stash:: apply ( path, num, None , None , sender) ,
85- )
86- } ) ;
50+ let apply_op = if let Some ( stash_num) = stash_num {
51+ ApplyOp :: Stash ( oid, stash_num, None , None )
8752 } else {
88- sender
89- . send_blocking ( crate :: Event :: CherryPick ( oid, false , None , None ) )
90- . expect ( "cant send through channel" ) ;
91- }
53+ ApplyOp :: CherryPick ( oid, None , None )
54+ } ;
55+ sender
56+ . send_blocking ( crate :: Event :: Apply ( apply_op) )
57+ . expect ( "cant send through channel" ) ;
9258 }
9359 } ) ;
9460 hb. pack_end ( & cherry_pick_btn) ;
@@ -104,7 +70,7 @@ pub fn headerbar_factory(
10470 revert_btn. connect_clicked ( {
10571 move |_| {
10672 sender
107- . send_blocking ( crate :: Event :: CherryPick ( oid, true , None , None ) )
73+ . send_blocking ( crate :: Event :: Apply ( ApplyOp :: Revert ( oid, None , None ) ) )
10874 . expect ( "cant send through channel" ) ;
10975 }
11076 } ) ;
@@ -241,7 +207,7 @@ impl commit::CommitDiff {
241207 . unwrap ( ) ;
242208 buffer. place_cursor ( & iter) ;
243209 }
244- self . diff . cursor ( & txt. buffer ( ) , iter. line ( ) , true , ctx) ;
210+ self . diff . cursor ( & txt. buffer ( ) , iter. line ( ) , ctx) ;
245211 txt. bind_highlights ( ctx) ;
246212 }
247213}
@@ -273,13 +239,7 @@ pub fn show_commit_window(
273239 let window = builder. build ( ) ;
274240 let scroll = ScrolledWindow :: new ( ) ;
275241
276- let hb = headerbar_factory (
277- repo_path. clone ( ) ,
278- & window. clone ( ) ,
279- main_sender. clone ( ) ,
280- oid,
281- stash_num,
282- ) ;
242+ let hb = headerbar_factory ( main_sender. clone ( ) , oid, stash_num) ;
283243
284244 let txt = crate :: stage_factory ( sender. clone ( ) , "commit_view" ) ;
285245
@@ -340,7 +300,6 @@ pub fn show_commit_window(
340300 ] ;
341301
342302 glib:: spawn_future_local ( {
343- let window = window. clone ( ) ;
344303 async move {
345304 while let Ok ( event) = receiver. recv ( ) . await {
346305 let mut ctx = crate :: StatusRenderContext :: new ( & txt) ;
@@ -366,6 +325,7 @@ pub fn show_commit_window(
366325 & mut labels,
367326 body_label. as_mut ( ) . unwrap ( ) ,
368327 ) ;
328+ cursor_position = CursorPosition :: from_context ( & ctx) ;
369329 // it should be called after cursor in ViewContainer
370330 diff. replace ( commit_diff) ;
371331 }
@@ -378,16 +338,19 @@ pub fn show_commit_window(
378338 buffer. iter_at_line ( d. diff . view . line_no . get ( ) ) . unwrap ( ) ;
379339 d. diff . render ( buffer, & mut iter, & mut ctx) ;
380340 let iter = buffer. iter_at_offset ( buffer. cursor_position ( ) ) ;
381- d. diff . cursor ( buffer, iter. line ( ) , true , & mut ctx) ;
341+ d. diff . cursor ( buffer, iter. line ( ) , & mut ctx) ;
382342 txt. bind_highlights ( & ctx) ;
343+ cursor_position = CursorPosition :: from_context ( & ctx) ;
383344 }
384345 }
385346 }
386347 Event :: Cursor ( _offset, line_no) => {
348+ info ! ( "Cursor!!!!!!!!!!!!!!!!!" ) ;
387349 if let Some ( d) = & mut diff {
388350 let buffer = & txt. buffer ( ) ;
389- d. diff . cursor ( buffer, line_no, false , & mut ctx) ;
351+ d. diff . cursor ( buffer, line_no, & mut ctx) ;
390352 cursor_position = CursorPosition :: from_context ( & ctx) ;
353+ info ! ( "GOT CURSOR POSITION {:?}" , cursor_position) ;
391354 }
392355 // it should be called after cursor in ViewContainer !!!!!!!!
393356 txt. bind_highlights ( & ctx) ;
@@ -405,31 +368,21 @@ pub fn show_commit_window(
405368 let buffer = txt. buffer ( ) ;
406369 let pos = buffer. cursor_position ( ) ;
407370 let iter = buffer. iter_at_offset ( pos) ;
408- debug ! ( "==========================" ) ;
409371 for tag in iter. tags ( ) {
410- println ! ( "Tag: {}" , tag. name( ) . unwrap( ) ) ;
372+ debug ! ( "Tag: {}" , tag. name( ) . unwrap( ) ) ;
411373 }
412374 }
413- Event :: Stage ( _) | Event :: RepoPopup => {
414- info ! ( "Stage/Unstage or r pressed" ) ;
375+ Event :: Stage ( op) if op != StageOp :: Kill => {
376+ info ! (
377+ "Stage/Unstage or r pressed {:?} cursor position {:?}" ,
378+ op, cursor_position
379+ ) ;
415380 if let Some ( diff) = & diff {
416- let title = if stash_num. is_some ( ) {
417- "Apply stash"
418- } else {
419- match event {
420- Event :: Stage ( _) => "Cherry pick" ,
421- _ => "Revert" ,
422- }
423- } ;
424- let ( body, file_path, hunk_header) = match cursor_position {
425- CursorPosition :: CursorDiff ( _) => ( oid. to_string ( ) , None , None ) ,
381+ let ( file_path, hunk_header) = match cursor_position {
382+ CursorPosition :: CursorDiff ( _) => ( None , None ) ,
426383 CursorPosition :: CursorFile ( _, Some ( file_idx) ) => {
427384 let file = & diff. diff . files [ file_idx] ;
428- (
429- format ! ( "File: {}" , file. path. to_str( ) . unwrap( ) ) ,
430- Some ( file. path . clone ( ) ) ,
431- None ,
432- )
385+ ( Some ( file. path . clone ( ) ) , None )
433386 }
434387 CursorPosition :: CursorHunk ( _, Some ( file_idx) , Some ( hunk_idx) )
435388 | CursorPosition :: CursorLine (
@@ -440,72 +393,28 @@ pub fn show_commit_window(
440393 ) => {
441394 let file = & diff. diff . files [ file_idx] ;
442395 let hunk = & file. hunks [ hunk_idx] ;
443- (
444- format ! (
445- "File: {}\n Applying single hunks is not yet implemented :(" ,
446- file. path. to_str( ) . unwrap( )
447- ) ,
448- Some ( file. path . clone ( ) ) ,
449- Some ( hunk. header . clone ( ) ) ,
450- )
396+ ( Some ( file. path . clone ( ) ) , Some ( hunk. header . clone ( ) ) )
451397 }
452- _ => ( "" . to_string ( ) , None , None ) ,
398+ _ => ( None , None ) ,
453399 } ;
454- let mut cherry_pick_handled = false ;
455- match event {
456- Event :: Stage ( _) => {
457- if stash_num. is_none ( ) {
458- cherry_pick_handled = true ;
459- main_sender
460- . send_blocking ( crate :: Event :: CherryPick (
461- oid,
462- false ,
463- file_path. clone ( ) ,
464- hunk_header. clone ( ) ,
465- ) )
466- . expect ( "cant send through channel" ) ;
400+ let apply_op = if let Some ( stash_num) = stash_num {
401+ ApplyOp :: Stash ( oid, stash_num, file_path, hunk_header)
402+ } else {
403+ match op {
404+ StageOp :: Stage => {
405+ ApplyOp :: CherryPick ( oid, file_path, hunk_header)
406+ }
407+ StageOp :: Unstage => {
408+ ApplyOp :: Revert ( oid, file_path, hunk_header)
409+ }
410+ _ => {
411+ unreachable ! ( "no way" )
467412 }
468413 }
469- _ => {
470- cherry_pick_handled = true ;
471- main_sender
472- . send_blocking ( crate :: Event :: CherryPick (
473- oid,
474- true ,
475- file_path. clone ( ) ,
476- hunk_header. clone ( ) ,
477- ) )
478- . expect ( "cant send through channel" ) ;
479- }
480- }
481- // temporary untill revert is not going via main event loop
482- if !cherry_pick_handled {
483- let path = repo_path. clone ( ) ;
484- let sender = main_sender. clone ( ) ;
485- let window = window. clone ( ) ;
486- glib:: spawn_future_local ( {
487- git_oid_op (
488- ConfirmDialog ( title. to_string ( ) , body. to_string ( ) ) ,
489- window,
490- move || match event {
491- Event :: Stage ( _) => {
492- if let Some ( stash_num) = stash_num {
493- stash:: apply (
494- path,
495- stash_num,
496- file_path,
497- hunk_header,
498- sender,
499- )
500- } else {
501- Ok ( ( ) )
502- }
503- }
504- _ => Ok ( ( ) ) ,
505- } ,
506- )
507- } ) ;
508- }
414+ } ;
415+ main_sender
416+ . send_blocking ( crate :: Event :: Apply ( apply_op) )
417+ . expect ( "cant send through channel" ) ;
509418 }
510419 }
511420 _ => {
0 commit comments