Skip to content

Commit

Permalink
code splits now uses new tiles, simple book option for TabsUnder
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Jul 20, 2024
1 parent 39cb513 commit cf28c99
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (cv *Code) Init() {
})

tree.AddChildAt(cv, "splits", func(w *core.Splits) {
w.SetSplits(cv.Settings.Splits...)
cv.ApplySplitsSettings(w)
tree.AddChildAt(w, "filetree", func(w *core.Frame) {
w.Styler(func(s *styles.Style) {
s.Direction = styles.Column
Expand Down
30 changes: 24 additions & 6 deletions code/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ type ProjectSettings struct { //types:add
Register RegisterName `display:"-"`

// current splitter splits
Splits []float32 `display:"-"`
Splits [4]float32 `display:"-"`

// current tabUnder setting for splits
TabsUnder bool `display:"-"`
}

func (se *ProjectSettings) Update() {
Expand Down Expand Up @@ -327,15 +330,17 @@ func OpenPaths() {
func (cv *Code) Defaults() {
cv.Settings.Files = Settings.Files
cv.Settings.Editor = core.SystemSettings.Editor
cv.Settings.Splits = []float32{.1, .325, .325, .25}
cv.Settings.Splits = [4]float32{.1, .325, .325, .25}
cv.Settings.TabsUnder = true
cv.Settings.Debug = cdebug.DefaultParams
}

// GrabSettings grabs the current project preference settings from various
// places, e.g., prior to saving or editing.
func (cv *Code) GrabSettings() {
sv := cv.Splits()
cv.Settings.Splits = sv.Splits()
copy(cv.Settings.Splits[:], sv.Splits())
cv.Settings.TabsUnder = len(sv.Tiles) == 2
cv.Settings.Dirs = cv.Files.Dirs
}

Expand All @@ -360,11 +365,21 @@ func (cv *Code) ApplySettings() {
cv.ConfigTextBuffer(ond.Buffer)
}
}
cv.Splits().SetSplits(cv.Settings.Splits...)
cv.ApplySplitsSettings(cv.Splits())
}
core.UpdateAll() // drives full rebuild
}

func (cv *Code) ApplySplitsSettings(sv *core.Splits) {
if cv.Settings.TabsUnder {
sv.SetTiles(core.TileSpan, core.TileSecondLong)
} else {
sv.SetTiles(core.TileSpan, core.TileSpan, core.TileSpan, core.TileSpan)
}
sv.SetSplits(cv.Settings.Splits[:]...)
sv.Update()
}

// ApplySettingsAction applies current settings to the project, and updates the project
func (cv *Code) ApplySettingsAction() {
cv.ApplySettings()
Expand Down Expand Up @@ -393,8 +408,10 @@ func (cv *Code) SplitsSetView(split SplitName) { //types:add
sv := cv.Splits()
sp, _, ok := AvailableSplits.SplitByName(split)
if ok {
sv.SetSplits(sp.Splits...).NeedsLayout()
cv.Settings.Splits = sp.Splits
cv.Settings.TabsUnder = sp.TabsUnder
cv.Settings.SplitName = split
cv.ApplySplitsSettings(sv)
if !cv.PanelIsOpen(cv.ActiveTextEditorIndex + TextEditor1Index) {
cv.SetActiveTextEditorIndex((cv.ActiveTextEditorIndex + 1) % 2)
}
Expand All @@ -416,7 +433,8 @@ func (cv *Code) SplitsSave(split SplitName) { //types:add
// saves to prefs file
func (cv *Code) SplitsSaveAs(name, desc string) { //types:add
sv := cv.Splits()
AvailableSplits.Add(name, desc, sv.Splits())
tabUnder := len(sv.Tiles) == 2
AvailableSplits.Add(name, desc, sv.Splits(), tabUnder)
AvailableSplits.SaveSettings()
}

Expand Down
40 changes: 15 additions & 25 deletions code/splits.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"path/filepath"
"slices"

"cogentcore.org/core/base/errors"
"cogentcore.org/core/base/fsx"
Expand All @@ -31,7 +30,11 @@ type Split struct {
Desc string

// splitter panel proportions
Splits []float32 `min:"0" max:"1" step:".05"` // TODO: convert to [4]float32 !!
Splits [4]float32 `min:"0" max:"1" step:".05"`

// TabsUnder sets the tabs under the editors, making a more compact layout,
// suitable for laptop and smaller displays.
TabsUnder bool
}

// Label satisfies the Labeler interface
Expand All @@ -41,11 +44,11 @@ func (sp Split) Label() string {

// SaveSplits saves given splits to this setting -- must use copy!
func (lt *Split) SaveSplits(sp []float32) {
copy(lt.Splits, sp)
copy(lt.Splits[:], sp)
}

// Splits is a list of named splitter configurations
type Splits []*Split
type Splits []*Split //types:add

// SplitName has an associated ValueView for selecting from the list of
// available named splits
Expand Down Expand Up @@ -79,8 +82,9 @@ func (lt *Splits) SplitByName(name SplitName) (*Split, int, bool) {
}

// Add adds a new splitter setting, returns split and index
func (lt *Splits) Add(name, desc string, splits []float32) (*Split, int) {
sp := &Split{Name: name, Desc: desc, Splits: slices.Clone(splits)}
func (lt *Splits) Add(name, desc string, splits []float32, tabsUnder bool) (*Split, int) {
sp := &Split{Name: name, Desc: desc, TabsUnder: tabsUnder}
copy(sp.Splits[:], splits)
*lt = append(*lt, sp)
return sp, len(*lt) - 1
}
Expand All @@ -98,24 +102,11 @@ func (lt *Splits) Names() []string {
// directory for saving / loading the default AvailSplits
var SplitsSettingsFilename = "splits-settings.json"

// FixLen ensures that there are exactly 4 splits in each
func (lt *Splits) FixLen() {
for _, sp := range *lt {
ln := len(sp.Splits)
if ln > 4 {
sp.Splits = sp.Splits[:4]
} else if ln < 4 {
sp.Splits = append(sp.Splits, make([]float32, 4-ln)...)
}
}
}

// Open opens named splits from a json-formatted file.
func (lt *Splits) Open(filename core.Filename) error { //types:add
if errors.Ignore1(fsx.FileExists(string(filename))) {
*lt = make(Splits, 0, 10) // reset
err := errors.Log(jsonx.Open(lt, string(filename)))
lt.FixLen()
return err
}
return nil
Expand All @@ -140,7 +131,6 @@ func (lt *Splits) OpenSettings() error { //types:add

// SaveSettings saves Splits to App standard prefs directory, using PrefSplitsFilename
func (lt *Splits) SaveSettings() error { //types:add
lt.FixLen()
pdir := core.TheApp.AppDataDir()
pnm := filepath.Join(pdir, SplitsSettingsFilename)
AvailableSplitsChanged = false
Expand All @@ -156,7 +146,6 @@ func (lt *Splits) CopyFrom(cp Splits) {
fmt.Printf("json err: %v\n", err.Error())
}
json.Unmarshal(b, lt)
lt.FixLen()
}

// AvailableSplitsChanged is used to update toolbars via following menu, toolbar
Expand All @@ -166,10 +155,11 @@ var AvailableSplitsChanged = false

// StandardSplits is the original compiled-in set of standard named splits.
var StandardSplits = Splits{
{"Code", "2 text views, tabs", []float32{.1, .325, .325, .25}},
{"Small", "1 text view, tabs", []float32{.1, .5, 0, .4}},
{"BigTabs", "1 text view, big tabs", []float32{.1, .3, 0, .6}},
{"Debug", "big command panel for debugging", []float32{0.1, 0.3, 0.3, 0.3}},
{"Compact", "2 text views, tabs under", [4]float32{.2, .5, .5, .3}, true},
{"Wide", "2 text views, tabs, in a row", [4]float32{.1, .325, .325, .25}, false},
{"Small", "1 text view, tabs", [4]float32{.2, 1, 0, .3}, true},
{"BigTabs", "1 text view, big tabs", [4]float32{.1, .3, 0, .6}, false},
{"Debug", "bigger command panel for debugging", [4]float32{0.1, 0.3, 0.3, 0.3}, false},
}

// SplitsView opens a view of a splits table
Expand Down
2 changes: 1 addition & 1 deletion code/typegen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf28c99

Please sign in to comment.