Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Patitotective committed Oct 25, 2023
1 parent 6c1c946 commit 2bf595e
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 302 deletions.
17 changes: 8 additions & 9 deletions ImExample.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ backend = "cpp"
# Dependencies

requires "nim >= 1.6.2"
requires "kdl >= 1.0.0"
requires "kdl >= 1.2.4"
requires "nimgl >= 1.3.2"
requires "stb_image >= 2.5"
requires "imstyle >= 1.0.0"
requires "openurl >= 2.0.3"
requires "tinydialogs >= 1.0.0"
requires "constructor >= 1.1.4"

import std/[strformat, options]
import src/types
import kdl

const configPath {.strdefine.} = "config.kdl"
const config = parseKdlFile(configPath).decode(Config)
const config = initConfig()

version = config.version
namedBin["main"] = config.name
Expand Down Expand Up @@ -59,12 +58,12 @@ task buildapp, "Build the AppImage":

# Make desktop file
writeFile(
&"AppDir/{config.name}.desktop",
&"AppDir/{config.name}.desktop",
desktop % [
"name", config.name,
"categories", config.categories.join(";"),
"version", config.version,
"comment", config.comment,
"name", config.name,
"categories", config.categories.join(";"),
"version", config.version,
"comment", config.comment,
"arch", arch
]
)
Expand Down
101 changes: 39 additions & 62 deletions main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import src/[settingsmodal, utils, types, icons]
when defined(release):
import resources

# TODO make configuration or at least settings inside the code and nto in the configuration file
# TODO be able to reset single preferences to their default value

# const configPath {.strdefine.} = "config.kdl"

proc getConfigDir(app: App): string =
getConfigDir() / app.config.name

Expand Down Expand Up @@ -79,7 +76,7 @@ proc drawMainMenuBar(app: var App) =

if igBeginMenu("Edit"):
if igMenuItem("Hello"):
echo "Hello"
discard messageBox(app.config.name, "Hello, earthling", DialogType.Ok, IconType.Info, Button.Yes)

igEndMenu()

Expand All @@ -95,7 +92,8 @@ proc drawMainMenuBar(app: var App) =

# See https://github.com/ocornut/imgui/issues/331#issuecomment-751372071
if openPrefs:
app.settingsmodal.cache = app.prefs[settings]
echo "TODO"
# app.settingsmodal.cache = app.prefs[settings]
igOpenPopup("Settings")
if openAbout:
igOpenPopup("###about")
Expand All @@ -120,7 +118,7 @@ proc drawMain(app: var App) = # Draw the main window

app.fonts[1].igPushFont()
igText("Unicode fonts (NotoSansJP-Regular.otf)")
igText("日本語がいいですね。 " & FA_SmileO)
igText("日本語の言葉 " & FA_SmileO)
igPopFont()

igEnd()
Expand Down Expand Up @@ -163,9 +161,10 @@ proc initWindow(app: var App) =
glfwWindowHint(GLFWMaximized, GLFW_TRUE)

app.win = glfwCreateWindow(
app.prefs[winsize].x,
app.prefs[winsize].y,
app.prefs[winsize].w,
app.prefs[winsize].h,
cstring app.config.name,
# glfwGetPrimaryMonitor(), # Show the window on the primary monitor
icon = false # Do not use default icon
)

Expand All @@ -176,16 +175,16 @@ proc initWindow(app: var App) =
var icon = initGLFWImage(app.res(app.config.iconPath).readImageFromMemory())
app.win.setWindowIcon(1, icon.addr)

if app.config.minSize.isSome:
app.win.setWindowSizeLimits(app.config.minSize.get.x, app.config.minSize.get.y, GLFW_DONT_CARE, GLFW_DONT_CARE) # minWidth, minHeight, maxWidth, maxHeight
# min width, min height, max widht, max height
app.win.setWindowSizeLimits(app.config.minSize.w, app.config.minSize.h, GLFW_DONT_CARE, GLFW_DONT_CARE)

# If negative pos, center the window in the first monitor
if app.prefs[winpos].x < 0 or app.prefs[winpos].y < 0:
var monitorX, monitorY, count, width, height: int32
let monitors = glfwGetMonitors(count.addr)
let videoMode = monitors[0].getVideoMode()
let monitor = glfwGetMonitors(count.addr)[0]#glfwGetPrimaryMonitor()
let videoMode = monitor.getVideoMode()

monitors[0].getMonitorPos(monitorX.addr, monitorY.addr)
monitor.getMonitorPos(monitorX.addr, monitorY.addr)
app.win.getWindowSize(width.addr, height.addr)
app.win.setWindowPos(
monitorX + int32((videoMode.width - width) / 2),
Expand All @@ -194,66 +193,43 @@ proc initWindow(app: var App) =
else:
app.win.setWindowPos(app.prefs[winpos].x, app.prefs[winpos].y)

proc initPrefs(app: var App) =
app.prefs = initKPrefs(
path = (app.getConfigDir() / "prefs").changeFileExt("kdl"),
default = Prefs(
maximized: false,
winpos: (-1i32, -1i32), # Negative numbers center the window
winsize: (600i32, 650i32),
settings: Settings(
input: "Hello World",
hintInput: "",
checkbox: true,
combo: Abc.C,
radio: Abc.A,
numbers: Numbers(
slider: 4,
floatSlider: 2.5,
spin: 4,
floatSpin: 3.14,
),
colors: Colors(
rgb: (1.0f, 0.0f, 0.2f),
rgba: (0.4f, 0.7f, 0.0f, 0.5f),
),
)
)
)

proc checkSettings(app: App) =
for name, field in app.settingsmodal.cache.fieldPairs:
var found = false
for key, _ in app.config.settings:
if key.eqIdent name:
found = true
break

if not found:
raise newException(KeyError, name & " is not in defined in the config file")

proc initApp(): App =
when defined(release):
result.resources = readResources()

result.checkSettings()
result.config = initConfig()

let filename =
when defined(release): "prefs"
else: "prefs_dev"

result.initPrefs()
result.prefs = initKPrefs(
path = (result.getConfigDir() / filename).changeFileExt("kdl"),
default = initPrefs()
)

template initFonts(app: var App) =
# Merge ForkAwesome icon font
let config = utils.newImFontConfig(mergeMode = true)
let ranges = [uint16 FA_Min, uint16 FA_Max]
let iconFontGlyphRanges = [uint16 FA_Min, uint16 FA_Max]

for e, (path, size) in app.config.fonts.fonts:
for e, font in app.config.fonts:
let glyph_ranges =
case e
of 1: io.fonts.getGlyphRangesJapanese()
else: nil

app.fonts[e] = io.fonts.igAddFontFromMemoryTTF(app.res(path), size, glyph_ranges = glyph_ranges)
if app.config.fonts.iconFontPath.len > 0:
io.fonts.igAddFontFromMemoryTTF(app.res(app.config.fonts.iconFontPath), size, config.unsafeAddr, ranges[0].unsafeAddr)
case font.glyphRanges
of GlyphRanges.Default: io.fonts.getGlyphRangesDefault()
of ChineseFull: io.fonts.getGlyphRangesChineseFull()
of ChineseSimplified: io.fonts.getGlyphRangesChineseSimplifiedCommon()
of Cyrillic: io.fonts.getGlyphRangesCyrillic()
of Japanese: io.fonts.getGlyphRangesJapanese()
of Korean: io.fonts.getGlyphRangesKorean()
of Thai: io.fonts.getGlyphRangesThai()
of Vietnamese: io.fonts.getGlyphRangesVietnamese()

app.fonts[e] = io.fonts.igAddFontFromMemoryTTF(app.res(font.path), font.size, glyph_ranges = glyph_ranges)

# Here we add the icon font to every font
if app.config.iconFontPath.len > 0:
io.fonts.igAddFontFromMemoryTTF(app.res(app.config.iconFontPath), font.size, config.unsafeAddr, iconFontGlyphRanges[0].unsafeAddr)

proc terminate(app: var App) =
var x, y, width, height: int32
Expand Down Expand Up @@ -309,3 +285,4 @@ proc main() =

when isMainModule:
main()

14 changes: 6 additions & 8 deletions resources.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import std/[sequtils, tables]
import kdl
import src/types

const configPath {.strdefine.} = "config.kdl"
const config = parseKdlFile(configPath).decode(Config)
const config = initConfig()

const resourcesPaths = @[
configPath,
config.stylePath,
config.iconPath,
config.fonts.iconFontPath,
] & config.fonts.fonts.mapIt(it.path)
config.stylePath,
config.iconPath,
config.iconFontPath,
] & config.fonts.mapIt(it.path)

proc readResources*(): Table[string, string] {.compileTime.} =
proc readResources*(): Table[string, string] {.compileTime.} =
for path in resourcesPaths:
result[path] = slurp(path)
Loading

0 comments on commit 2bf595e

Please sign in to comment.