Skip to content

Commit

Permalink
Change ActivateEditor signature
Browse files Browse the repository at this point in the history
changes ActivateEditor signature and extend it to RichText editors.
  • Loading branch information
aarzilli committed Sep 29, 2023
1 parent 1cf8a07 commit 6210113
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 39 deletions.
46 changes: 27 additions & 19 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -221,14 +219,21 @@ 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 {
clicked = true
break
}
}
if !clicked {
if !clicked && ctx.Input.activateEditorWindow == nil {
return
}
ctx.dockedWindowFocus = 0
Expand All @@ -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]
Expand All @@ -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
})
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type KeyboardInput struct {
type Input struct {
Keyboard KeyboardInput
Mouse MouseInput

activateEditor interface{}
activateEditorWindow *Window
}

func (win *Window) Input() *Input {
Expand Down
17 changes: 11 additions & 6 deletions masterwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MasterWindow interface {
Close()
Closed() bool
OnClose(func())
ActivateEditor(ed *TextEditor)
ActivateEditor(*Window, interface{})

Style() *nstyle.Style
SetStyle(*nstyle.Style)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion richtext/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions richtext/rtxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6210113

Please sign in to comment.