Skip to content

Commit

Permalink
Added stable background mode
Browse files Browse the repository at this point in the history
Added BackgroundMode:stable command to prevent the editor from changing the
background colour to indicate the mode. This is mostly useful for writing prose,
or documentation, where you're changing modes so frequently that the flashing
gets distracting. It can also be useful for people who delete many lines by
holding down the `d` key instead of using <n>dd.
  • Loading branch information
driusan committed Jul 15, 2016
1 parent 720ccd4 commit 1116059
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ executing words.
to print line numbers relative to the cursor, or LineNumbers:absolute to
print absolute line numbers. LineNumbers with no arguments will toggle
the mode in the order off->relative->absolute->off
* `BackgroundMode`: Sets whether or not the background should change colour to
indicate the keyboard mode. BackgroundMode:stable will prevent the
background from changing colour. Anything else will restore the default
mode where delete mode is red, and insert mode is a light greenish/blue
colour.

## Experimental Commands

Expand Down
11 changes: 10 additions & 1 deletion actions/defaults/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func init() {
actions.RegisterAction("TermWidth", TermWidth)
actions.RegisterAction("WarnAlpha", WarnAlpha)
actions.RegisterAction("LineNumbers", LineNumberMode)

actions.RegisterAction("BackgroundMode", BackgroundMode)
}

func TermWidth(args string, buff *demodel.CharBuffer, v demodel.Viewport) {
Expand Down Expand Up @@ -52,3 +52,12 @@ func LineNumberMode(args string, buff *demodel.CharBuffer, v demodel.Viewport) {
v.SetOption("RotateLineNumbers", nil)
}
}

func BackgroundMode(args string, buff *demodel.CharBuffer, v demodel.Viewport) {
switch args {
case "stable":
v.SetOption("BackgroundMode", viewer.StableBackground)
default:
v.SetOption("BackgroundMode", viewer.DefaultBackground)
}
}
21 changes: 20 additions & 1 deletion viewer/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ const (
AbsoluteLineNumbers
)

// BackgroundMode represents whether or not the viewport background should
// change colour depending on the mode. It can be disabled by setting the
// viewport mode to StableBackground.
type BackgroundMode uint8

const (
// Background is context-sensitive
DefaultBackground = BackgroundMode(iota)
// background does not change colour
StableBackground
)

type Viewport struct {
demodel.Map
demodel.Renderer
Expand All @@ -39,6 +51,7 @@ type Viewport struct {
warnalpha uint8

lineNumberMode LineNumberMode
BackgroundMode BackgroundMode
}

type RequestRerender struct{}
Expand Down Expand Up @@ -129,11 +142,17 @@ func (v *Viewport) SetOption(opt string, val interface{}) error {
v.lineNumberMode = NoLineNumbers
}
return nil
case "BackgroundMode":
if m, ok := val.(BackgroundMode); ok {
v.BackgroundMode = m
} else {
v.BackgroundMode = DefaultBackground
}
return nil
default:
return demodel.ErrUnsupportedOption
}
}

func mult(x, y fixed.Int26_6) fixed.Int26_6 {
// multiplying two fixed-point precision values requires shifting
// the decimal 6 places to get it back to the right place. This will
Expand Down
19 changes: 13 additions & 6 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@ func (w *dewindow) realpaint(buf *demodel.CharBuffer, viewport *viewer.Viewport)

// Fill the buffer with the window background colour before
// drawing the web page on top of it.
switch viewport.GetKeyboardMode() {
case kbmap.InsertMode:
draw.Draw(dst, dst.Bounds(), &image.Uniform{renderer.InsertBackground}, image.ZP, draw.Src)
case kbmap.DeleteMode:
draw.Draw(dst, dst.Bounds(), &image.Uniform{renderer.DeleteBackground}, image.ZP, draw.Src)
default:
// This should logically be in the viewport code itself, but importing
// kbmap to switch on the mode sentinals would result in a cyclical
// import.
if viewport.BackgroundMode != viewer.StableBackground {
switch viewport.GetKeyboardMode() {
case kbmap.InsertMode:
draw.Draw(dst, dst.Bounds(), &image.Uniform{renderer.InsertBackground}, image.ZP, draw.Src)
case kbmap.DeleteMode:
draw.Draw(dst, dst.Bounds(), &image.Uniform{renderer.DeleteBackground}, image.ZP, draw.Src)
default:
draw.Draw(dst, dst.Bounds(), &image.Uniform{renderer.NormalBackground}, image.ZP, draw.Src)
}
} else {
draw.Draw(dst, dst.Bounds(), &image.Uniform{renderer.NormalBackground}, image.ZP, draw.Src)
}

Expand Down

0 comments on commit 1116059

Please sign in to comment.