Correct menu bar+open files from os.Args

This commit is contained in:
bloeys
2022-02-24 19:28:04 +04:00
parent 6a4b9fd1b3
commit 4570dbddce
3 changed files with 149 additions and 51 deletions

View File

@ -1,30 +0,0 @@
[Window][Debug##Default]
Pos=306,2
Size=400,400
Collapsed=0
[Window][text]
Pos=60,60
Size=32,32
Collapsed=0
[Window][sidebar]
Pos=0,0
Size=128,720
Collapsed=0
[Window][editor]
Pos=128,0
Size=1152,38
Collapsed=0
[Window][editorText]
Pos=128,38
Size=1152,682
Collapsed=0
[Window][err]
Pos=36,-20
Size=3072,1657
Collapsed=0

111
main.go
View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path" "path/filepath"
"github.com/bloeys/nmage/engine" "github.com/bloeys/nmage/engine"
"github.com/bloeys/nmage/input" "github.com/bloeys/nmage/input"
@ -29,7 +29,9 @@ type Gopad struct {
ImGUIInfo nmageimgui.ImguiInfo ImGUIInfo nmageimgui.ImguiInfo
Quitting bool Quitting bool
sidebarSize float32 mainMenuBarHeight float32
sidebarWidthFactor float32
sidebarWidthPx float32
CurrDir string CurrDir string
CurrDirContents []fs.DirEntry CurrDirContents []fs.DirEntry
@ -46,12 +48,21 @@ type Gopad struct {
//Settings //Settings
textSelectionColor imgui.Vec4 textSelectionColor imgui.Vec4
editorBgColor imgui.Vec4 editorBgColor imgui.Vec4
//Cache window size
winWidth float32
winHeight float32
} }
var () var ()
func main() { func main() {
chdirErr := os.Chdir(filepath.Dir(os.Args[0]))
if chdirErr != nil {
panic(chdirErr.Error())
}
if err := engine.Init(); err != nil { if err := engine.Init(); err != nil {
panic(err) panic(err)
} }
@ -75,6 +86,8 @@ func main() {
{fileName: "**scratch**"}, {fileName: "**scratch**"},
}, },
editorToClose: -1, editorToClose: -1,
sidebarWidthFactor: 0.15,
textSelectionColor: imgui.Vec4{X: 84 / 255.0, Y: 153 / 255.0, Z: 199 / 255.0, W: 0.4}, textSelectionColor: imgui.Vec4{X: 84 / 255.0, Y: 153 / 255.0, Z: 199 / 255.0, W: 0.4},
editorBgColor: imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1}, editorBgColor: imgui.Vec4{X: 0.1, Y: 0.1, Z: 0.1, W: 1},
} }
@ -85,6 +98,7 @@ func main() {
func (g *Gopad) Init() { func (g *Gopad) Init() {
g.Win.SDLWin.SetTitle("Gopad")
g.Win.EventCallbacks = append(g.Win.EventCallbacks, g.handleWindowEvents) g.Win.EventCallbacks = append(g.Win.EventCallbacks, g.handleWindowEvents)
//Setup font //Setup font
@ -99,9 +113,28 @@ func (g *Gopad) Init() {
//Sidebar //Sidebar
g.CurrDirContents = getDirContents(g.CurrDir) g.CurrDirContents = getDirContents(g.CurrDir)
w, _ := g.Win.SDLWin.GetSize() w, h := g.Win.SDLWin.GetSize()
g.sidebarSize = float32(w) * 0.10 g.winWidth = float32(w)
g.winHeight = float32(h)
g.sidebarWidthPx = g.winWidth * g.sidebarWidthFactor
//Read os.Args
for i := 1; i < len(os.Args); i++ {
b, err := os.ReadFile(os.Args[i])
if err != nil {
errMsg := "Error opening file. Error: " + err.Error()
println(errMsg)
continue
}
g.editors = append(g.editors, Editor{
fileName: filepath.Base(os.Args[i]),
filePath: os.Args[i],
fileContents: string(b),
})
}
g.activeEditor = len(g.editors) - 1
} }
func (g *Gopad) handleWindowEvents(event sdl.Event) { func (g *Gopad) handleWindowEvents(event sdl.Event) {
@ -112,8 +145,10 @@ func (g *Gopad) handleWindowEvents(event sdl.Event) {
case *sdl.TextInputEvent: case *sdl.TextInputEvent:
case *sdl.WindowEvent: case *sdl.WindowEvent:
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED { if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
w, _ := g.Win.SDLWin.GetSize() w, h := g.Win.SDLWin.GetSize()
g.sidebarSize = float32(w) * 0.15 g.winWidth = float32(w)
g.winHeight = float32(h)
g.sidebarWidthPx = g.winWidth * g.sidebarWidthFactor
} }
} }
} }
@ -154,6 +189,11 @@ func (g *Gopad) Update() {
return return
} }
g.saveEditor(e)
}
func (g *Gopad) saveEditor(e *Editor) {
if e.fileName == "**scratch**" { if e.fileName == "**scratch**" {
e.isModified = false e.isModified = false
return return
@ -161,14 +201,14 @@ func (g *Gopad) Update() {
err := os.WriteFile(e.filePath, []byte(e.fileContents), os.ModePerm) err := os.WriteFile(e.filePath, []byte(e.fileContents), os.ModePerm)
if err != nil { if err != nil {
g.fireError("Failed to save file. Error: " + err.Error()) g.triggerError("Failed to save file. Error: " + err.Error())
return return
} }
e.isModified = false e.isModified = false
} }
func (g *Gopad) fireError(errMsg string) { func (g *Gopad) triggerError(errMsg string) {
imgui.OpenPopup("err") imgui.OpenPopup("err")
g.haveErr = true g.haveErr = true
g.errMsg = errMsg g.errMsg = errMsg
@ -199,17 +239,42 @@ func (g *Gopad) Render() {
//Global imgui settings //Global imgui settings
imgui.PushFont(g.mainFont) imgui.PushFont(g.mainFont)
g.drawMenubar()
g.drawSidebar() g.drawSidebar()
g.drawEditors() g.drawEditors()
imgui.PopFont() imgui.PopFont()
} }
func (g *Gopad) drawMenubar() {
// imgui.SetNextWindowPos(imgui.Vec2{X: 0, Y: 0})
// imgui.SetNextWindowSize(imgui.Vec2{X: g.winWidth})
// imgui.BeginV("menuBar", nil, imgui.WindowFlagsMenuBar|imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoTitleBar|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove|imgui.WindowFlagsNoResize)
shouldCloseMenuBar := imgui.BeginMainMenuBar()
if imgui.BeginMenu("File") {
if imgui.MenuItem("Save") {
g.saveEditor(g.getActiveEditor())
}
imgui.EndMenu()
}
g.mainMenuBarHeight = imgui.WindowHeight()
if shouldCloseMenuBar {
imgui.EndMainMenuBar()
}
// imgui.End()
}
func (g *Gopad) drawSidebar() { func (g *Gopad) drawSidebar() {
_, h := g.Win.SDLWin.GetSize() imgui.SetNextWindowPos(imgui.Vec2{X: 0, Y: g.mainMenuBarHeight})
imgui.SetNextWindowPos(imgui.Vec2{X: 0, Y: 0}) imgui.SetNextWindowSize(imgui.Vec2{X: g.sidebarWidthPx, Y: g.winHeight - g.mainMenuBarHeight})
imgui.SetNextWindowSize(imgui.Vec2{X: g.sidebarSize, Y: float32(h)})
imgui.BeginV("sidebar", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove) imgui.BeginV("sidebar", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
imgui.PushStyleColor(imgui.StyleColorButton, imgui.Vec4{W: 0}) imgui.PushStyleColor(imgui.StyleColorButton, imgui.Vec4{W: 0})
@ -230,20 +295,21 @@ func (g *Gopad) drawSidebar() {
func (g *Gopad) drawEditors() { func (g *Gopad) drawEditors() {
//Draw editor area window //Draw editor area window
w, h := g.Win.SDLWin.GetSize() imgui.SetNextWindowPos(imgui.Vec2{X: g.sidebarWidthPx, Y: g.mainMenuBarHeight})
imgui.SetNextWindowPos(imgui.Vec2{X: g.sidebarSize, Y: 0}) imgui.SetNextWindowSize(imgui.Vec2{X: g.winWidth - g.sidebarWidthPx})
imgui.SetNextWindowSize(imgui.Vec2{X: float32(w) - g.sidebarSize})
imgui.BeginV("editor", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove) imgui.BeginV("editor", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
//Draw tabs //Draw tabs
isEditorsEnabled := imgui.BeginTabBarV("editorTabs", 0) isEditorsEnabled := imgui.BeginTabBarV("editorTabs", 0)
shouldForceSwitch := g.activeEditor != g.lastActiveEditor
prevActiveEditor := g.activeEditor
for i := 0; i < len(g.editors); i++ { for i := 0; i < len(g.editors); i++ {
e := &g.editors[i] e := &g.editors[i]
shouldForceSwitch := g.activeEditor == i && g.activeEditor != g.lastActiveEditor
flags := imgui.TabItemFlagsNone flags := imgui.TabItemFlagsNone
if shouldForceSwitch { if shouldForceSwitch && g.activeEditor == i {
flags = imgui.TabItemFlagsSetSelected flags = imgui.TabItemFlagsSetSelected
} }
@ -281,15 +347,21 @@ func (g *Gopad) drawEditors() {
imgui.End() imgui.End()
//Draw text area //Draw text area
fullWinSize := imgui.Vec2{X: float32(w) - g.sidebarSize, Y: float32(h) - tabsHeight} imgui.SetNextWindowPos(imgui.Vec2{X: g.sidebarWidthPx, Y: g.mainMenuBarHeight + tabsHeight})
imgui.SetNextWindowPos(imgui.Vec2{X: g.sidebarSize, Y: tabsHeight})
fullWinSize := imgui.Vec2{X: g.winWidth - g.sidebarWidthPx, Y: g.winHeight - g.mainMenuBarHeight - tabsHeight}
imgui.SetNextWindowSize(fullWinSize) imgui.SetNextWindowSize(fullWinSize)
imgui.BeginV("editorText", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove) imgui.BeginV("editorText", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
imgui.PushStyleColor(imgui.StyleColorFrameBg, g.editorBgColor) imgui.PushStyleColor(imgui.StyleColorFrameBg, g.editorBgColor)
imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, g.textSelectionColor) imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, g.textSelectionColor)
fullWinSize.Y -= 18 fullWinSize.Y -= 18
if shouldForceSwitch || prevActiveEditor != g.activeEditor {
imgui.SetKeyboardFocusHereV(1)
}
imgui.InputTextMultilineV("", &g.getActiveEditor().fileContents, fullWinSize, imgui.ImGuiInputTextFlagsCallbackEdit|imgui.InputTextFlagsAllowTabInput, g.textEditCB) imgui.InputTextMultilineV("", &g.getActiveEditor().fileContents, fullWinSize, imgui.ImGuiInputTextFlagsCallbackEdit|imgui.InputTextFlagsAllowTabInput, g.textEditCB)
imgui.PopStyleColor() imgui.PopStyleColor()
imgui.PopStyleColor() imgui.PopStyleColor()
imgui.End() imgui.End()
@ -372,7 +444,7 @@ func (g *Gopad) handleFileClick(fPath string) {
} }
g.editors = append(g.editors, Editor{ g.editors = append(g.editors, Editor{
fileName: path.Base(fPath), fileName: filepath.Base(fPath),
filePath: fPath, filePath: fPath,
fileContents: string(b), fileContents: string(b),
}) })
@ -380,7 +452,6 @@ func (g *Gopad) handleFileClick(fPath string) {
} }
func (g *Gopad) FrameEnd() { func (g *Gopad) FrameEnd() {
} }
func (g *Gopad) ShouldRun() bool { func (g *Gopad) ShouldRun() bool {

57
selectFolderModel.go Executable file
View File

@ -0,0 +1,57 @@
package main
import (
"os"
"strings"
"github.com/inkyblackness/imgui-go/v4"
)
func selectFolder(startDir string, winWidth float32, winHeight float32) (path string, done bool) {
if strings.TrimSpace(startDir) == "" {
var err error
startDir, err = os.UserHomeDir()
if err != nil {
panic(err.Error())
}
}
imgui.OpenPopup("selectFolder")
imgui.SetNextWindowPos(imgui.Vec2{X: float32(winWidth) * 0.5, Y: float32(winHeight) * 0.5})
shouldEnd := imgui.BeginPopupModalV("selectFolder", nil, imgui.WindowFlagsNoCollapse)
drawDir(startDir, true)
if shouldEnd {
imgui.EndPopup()
} else {
done = true
}
return path, done
}
func drawDir(fPath string, foldersOnly bool) {
// contents, err := os.ReadDir(fPath)
// if err != nil {
// panic(err)
// }
// for _, c := range contents {
// if !c.IsDir() {
// continue
// }
// isEnabled := imgui.TreeNodeV( dir.Name(), imgui.TreeNodeFlagsSpanAvailWidth)
// if !isEnabled {
// return
// }
// imgui.bu
// }
}