From 6210113f5d2d91fe7361c47cd14b0ade8f6cfe7c Mon Sep 17 00:00:00 2001 From: aarzilli Date: Fri, 29 Sep 2023 16:31:42 +0200 Subject: [PATCH] Change ActivateEditor signature changes ActivateEditor signature and extend it to RichText editors. --- context.go | 46 +++++++++++++++++++++++++++------------------- input.go | 3 +++ masterwindow.go | 17 +++++++++++------ richtext/draw.go | 2 +- richtext/rtxt.go | 11 ++++++++--- text.go | 13 +++---------- 6 files changed, 53 insertions(+), 39 deletions(-) diff --git a/context.go b/context.go index 102a852..43b4de0 100644 --- a/context.go +++ b/context.go @@ -22,16 +22,15 @@ const dumpFrame = false var UnknownCommandErr = errors.New("unknown command") type context struct { - mw MasterWindow - Input Input - Style nstyle.Style - Windows []*Window - DockedWindows dockedTree - changed int32 - activateEditor *TextEditor - cmds []command.Command - trashFrame bool - autopos image.Point + mw MasterWindow + Input Input + Style nstyle.Style + Windows []*Window + DockedWindows dockedTree + changed int32 + cmds []command.Command + trashFrame bool + autopos image.Point finalCmds command.Buffer @@ -208,7 +207,6 @@ func (ctx *context) Reset() { return w }) } - ctx.activateEditor = nil in := &ctx.Input in.Mouse.Buttons[mouse.ButtonLeft].Clicked = false in.Mouse.Buttons[mouse.ButtonMiddle].Clicked = false @@ -221,6 +219,13 @@ func (ctx *context) Reset() { } func (ctx *context) Restack() { + defer func() { + if ctx.Input.activateEditorWindow != nil { + ctx.Input.activateEditorWindow = nil + } else { + ctx.Input.activateEditor = nil + } + }() clicked := false for _, b := range []mouse.Button{mouse.ButtonLeft, mouse.ButtonRight, mouse.ButtonMiddle} { if ctx.Input.Mouse.Buttons[b].Clicked && ctx.Input.Mouse.Buttons[b].Down { @@ -228,7 +233,7 @@ func (ctx *context) Restack() { break } } - if !clicked { + if !clicked && ctx.Input.activateEditorWindow == nil { return } ctx.dockedWindowFocus = 0 @@ -252,7 +257,7 @@ func (ctx *context) Restack() { if ctx.Windows[i].flags&windowTooltip != 0 { continue } - if ctx.restackClick(ctx.Windows[i]) { + if ctx.restackClick(ctx.Windows[i]) || ctx.Input.activateEditorWindow == ctx.Windows[i] { found = true if toplevelIdx != i { newToplevel := ctx.Windows[i] @@ -275,6 +280,9 @@ func (ctx *context) Restack() { if ctx.restackClick(w) && (w.flags&windowDocked != 0) { ctx.dockedWindowFocus = w.idx } + if ctx.Input.activateEditorWindow == w { + ctx.dockedWindowFocus = w.idx + } return w }) } @@ -305,24 +313,24 @@ func (ctx *context) FindFocus() { } func (ctx *context) Walk(fn WindowWalkFn) { - fn(ctx.Windows[0].title, ctx.Windows[0].Data, false, 0, ctx.Windows[0].Bounds) + fn(ctx.Windows[0], ctx.Windows[0].title, ctx.Windows[0].Data, false, 0, ctx.Windows[0].Bounds) ctx.DockedWindows.walkExt(func(t *dockedTree) { switch t.Type { case dockedNodeHoriz: - fn("", nil, true, t.Split.Size, rect.Rect{}) + fn(t.W, "", nil, true, t.Split.Size, rect.Rect{}) case dockedNodeVert: - fn("", nil, true, -t.Split.Size, rect.Rect{}) + fn(t.W, "", nil, true, -t.Split.Size, rect.Rect{}) case dockedNodeLeaf: if t.W == nil { - fn("", nil, true, 0, rect.Rect{}) + fn(nil, "", nil, true, 0, rect.Rect{}) } else { - fn(t.W.title, t.W.Data, true, 0, t.W.Bounds) + fn(t.W, t.W.title, t.W.Data, true, 0, t.W.Bounds) } } }) for _, win := range ctx.Windows[1:] { if win.flags&WindowNonmodal != 0 { - fn(win.title, win.Data, false, 0, win.Bounds) + fn(win, win.title, win.Data, false, 0, win.Bounds) } } } diff --git a/input.go b/input.go index 880d163..0eeff93 100644 --- a/input.go +++ b/input.go @@ -33,6 +33,9 @@ type KeyboardInput struct { type Input struct { Keyboard KeyboardInput Mouse MouseInput + + activateEditor interface{} + activateEditorWindow *Window } func (win *Window) Input() *Input { diff --git a/masterwindow.go b/masterwindow.go index 1a2bb7a..977f78c 100644 --- a/masterwindow.go +++ b/masterwindow.go @@ -24,7 +24,7 @@ type MasterWindow interface { Close() Closed() bool OnClose(func()) - ActivateEditor(ed *TextEditor) + ActivateEditor(*Window, interface{}) Style() *nstyle.Style SetStyle(*nstyle.Style) @@ -47,7 +47,7 @@ func NewMasterWindow(flags WindowFlags, title string, updatefn UpdateFn) MasterW return NewMasterWindowSize(flags, title, image.Point{640, 480}, updatefn) } -type WindowWalkFn func(title string, data interface{}, docked bool, splitSize int, rect rect.Rect) +type WindowWalkFn func(w *Window, title string, data interface{}, docked bool, splitSize int, rect rect.Rect) type masterWindowCommon struct { ctx *context @@ -92,12 +92,17 @@ func (mw *masterWindowCommon) Input() *Input { return &mw.ctx.Input } -func (mw *masterWindowCommon) ActivateEditor(ed *TextEditor) { - mw.ctx.activateEditor = ed +func (mw *masterWindowCommon) ActivateEditor(win *Window, ed interface{}) { + mw.ctx.Input.activateEditor = ed + mw.ctx.Input.activateEditorWindow = win + mw.Changed() } -func (mw *masterWindowCommon) ActivatingEditor() *TextEditor { - return mw.ctx.activateEditor +func (mw *masterWindowCommon) ActivatingEditor() interface{} { + if mw.ctx.Input.activateEditorWindow == nil { + return mw.ctx.Input.activateEditor + } + return nil } func (mw *masterWindowCommon) Style() *nstyle.Style { diff --git a/richtext/draw.go b/richtext/draw.go index d42f8e0..b13a9d6 100644 --- a/richtext/draw.go +++ b/richtext/draw.go @@ -48,7 +48,7 @@ func (rtxt *RichText) drawWidget(w *nucular.Window) *Ctor { } type activateEditor interface { - ActivatingEditor() *nucular.TextEditor + ActivatingEditor() interface{} } func (rtxt *RichText) drawRows(w *nucular.Window, viewporth int) *Ctor { diff --git a/richtext/rtxt.go b/richtext/rtxt.go index 324debb..54e664e 100644 --- a/richtext/rtxt.go +++ b/richtext/rtxt.go @@ -459,9 +459,14 @@ func (rtxt *RichText) initialize(w *nucular.Window, changed *bool) { rtxt.Sel.E = 0 } if mw, _ := w.Master().(activateEditor); mw != nil && mw.ActivatingEditor() != nil { - rtxt.focused = false - rtxt.Sel.S = 0 - rtxt.Sel.E = 0 + if mw.ActivatingEditor() == rtxt { + rtxt.focused = true + rtxt.Group.grab(rtxt, w) + } else { + rtxt.focused = false + rtxt.Sel.S = 0 + rtxt.Sel.E = 0 + } } rtxt.arrowKey, rtxt.pageKey = 0, 0 diff --git a/text.go b/text.go index 00c7f24..0f3943f 100644 --- a/text.go +++ b/text.go @@ -1102,15 +1102,8 @@ func (ed *TextEditor) doEdit(bounds rect.Rect, style *nstyle.Edit, inp *Input, c /* update edit state */ prev_state := ed.Active - if ed.win.ctx.activateEditor != nil { - if ed.win.ctx.activateEditor == ed { - ed.Active = true - if ed.win.flags&windowDocked != 0 { - ed.win.ctx.dockedWindowFocus = ed.win.idx - } - } else { - ed.Active = false - } + if ed.win.ctx.Input.activateEditor != nil { + ed.Active = ed.win.ctx.Input.activateEditor == ed } is_hovered := inp.Mouse.HoveringRect(bounds) @@ -1237,7 +1230,7 @@ func (ed *TextEditor) doEdit(bounds rect.Rect, style *nstyle.Edit, inp *Input, c if ed.Flags&EditCtrlEnterNewline != 0 && e.Modifiers&key.ModShift != 0 { ed.Text([]rune{'\n'}) cursor_follow = true - } else if ed.Flags&EditSigEnter != 0 { + } else if ed.Flags&EditSigEnter != 0 && e.Modifiers == 0 { ret = EditInactive ret |= EditDeactivated if ed.Flags&EditReadOnly == 0 {