Skip to content

Commit

Permalink
refactor: simplify by merging most os specific code
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed May 17, 2024
1 parent 5f3549d commit 85aa26e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 96 deletions.
64 changes: 52 additions & 12 deletions src/_c.v → src/_common.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@ struct C.osdialog_color {

fn C.osdialog_message(level int, buttons int, message &char) int
fn C.osdialog_prompt(level int, message &char, text &char) &char
fn C.osdialog_file(action int, path &char, filename &char, filters C.osdialog_filters) &char
fn C.osdialog_color_picker(color &C.osdialog_color, opacity int) int
fn C.osdialog_file(action int, path &char, filename &char, filters C.osdialog_filters) &char

fn dialog_c__file_dialog(action FileAction, path &char, filename &char) ?string {
selected := C.osdialog_file(int(action), path, filename, unsafe { nil })
unsafe {
if selected != nil {
return selected.vstring()
fn dialog__message(message string, opts MessageOptions) bool {
$if macos {
app := create()
defer {
destroy(app)
}
}
return none
}

fn dialog_c__message(message string, opts MessageOptions) bool {
return if C.osdialog_message(int(opts.level), int(opts.buttons), &char(message.str)) == 1 {
true
} else {
false
}
}

fn dialog_c__prompt(message string, opts PromptOptions) ?string {
fn dialog__prompt(message string, opts PromptOptions) ?string {
$if macos {
app := create()
defer {
destroy(app)
}
}
input := C.osdialog_prompt(int(opts.level), &char(message.str), &char(opts.text.str))
unsafe {
if input != nil {
Expand All @@ -58,10 +60,48 @@ fn dialog_c__prompt(message string, opts PromptOptions) ?string {
return none
}

fn dialog_c__color_picker(opts ColorPickerOptions) ?Color {
fn dialog__color_picker(opts ColorPickerOptions) ?Color {
$if macos {
app := create()
defer {
destroy(app)
}
}
color := &opts.color
if C.osdialog_color_picker(color, int(opts.opacity)) == 1 {
return *color
}
return none
}

fn dialog__file_dialog_handler(action FileAction, path &char, filename &char) ?string {
$if macos {
app := create()
defer {
destroy(app)
}
}
selected := C.osdialog_file(int(action), path, filename, unsafe { nil })
unsafe {
if selected != nil {
return selected.vstring()
}
}
return none
}

fn dialog__file_dialog(opts FileDialogOptions) ?string {
return dialog__file_dialog_handler(opts.action, &char(opts.path.str), &char(opts.filename.str))
}

fn dialog__open_file(opts FileOpenOptions) ?string {
return dialog__file_dialog_handler(.open, &char(opts.path.str), unsafe { nil })
}

fn dialog__open_dir(opts FileOpenOptions) ?string {
return dialog__file_dialog_handler(.open_dir, &char(opts.path.str), unsafe { nil })
}

fn dialog__save_file(opts FileSaveOptions) ?string {
return dialog__file_dialog_handler(.save, &char(opts.path.str), &char(opts.filename.str))
}
29 changes: 0 additions & 29 deletions src/_default.c.v

This file was deleted.

60 changes: 5 additions & 55 deletions src/_macos.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,12 @@ module dialog

import webview

fn dialog__message(message string, opts MessageOptions) bool {
w := webview.create()
defer {
w.destroy()
}
return dialog_c__message(message, opts)
}

fn dialog__prompt(message string, opts PromptOptions) ?string {
w := webview.create()
defer {
w.destroy()
}
return dialog_c__prompt(message, opts)
}

fn dialog__file_dialog(opts FileDialogOptions) ?string {
w := webview.create()
defer {
w.destroy()
}
return dialog_c__file_dialog(opts.action, &char(opts.path.str), &char(opts.filename.str))
}

fn dialog__open_file(opts FileOpenOptions) ?string {
w := webview.create()
defer {
w.destroy()
}
return dialog_c__file_dialog(.open, &char(opts.path.str), unsafe { nil })
}

fn dialog__open_dir(opts FileOpenOptions) ?string {
w := webview.create()
defer {
w.destroy()
}
return dialog_c__file_dialog(.open_dir, &char(opts.path.str), unsafe { nil })
}
type App = &webview.Webview

fn dialog__save_file(opts FileSaveOptions) ?string {
w := webview.create()
defer {
w.destroy()
}
return dialog_c__file_dialog(.save, &char(opts.path.str), &char(opts.filename.str))
fn create() &webview.Webview {
return webview.create()
}

fn dialog__color_picker(opts ColorPickerOptions) ?Color {
w := webview.create()
defer {
w.destroy()
}
color := &opts.color
if C.osdialog_color_picker(color, int(opts.opacity)) == 1 {
return *color
}
return none
fn destroy(app &webview.Webview) {
app.destroy()
}

0 comments on commit 85aa26e

Please sign in to comment.