From 00eac4d5d6505f60ff047196856ecdf1d40eecdb Mon Sep 17 00:00:00 2001 From: Ronald Date: Sun, 29 Jun 2025 11:46:14 +0100 Subject: [PATCH] Add support for opening files with a dialog box This also tidies up some of the code for saving files, the dialogs are opened in a new thread so that we can continue to render the application in the main thread --- main.odin | 55 ++++++++++++++++++++++++++++++++++++------------------ types.odin | 6 +++--- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/main.odin b/main.odin index 4b991e2..34f56da 100644 --- a/main.odin +++ b/main.odin @@ -55,14 +55,14 @@ main :: proc() { // Load files from command line arguments if len(args) > 1 { for filename in args { - open_file(&app, filename) + create_file_buffer(&app, filename) } } else { - open_file(&app) + create_file_buffer(&app) } if len(app.files) == 0 { - fmt.println("No files could be loaded") + log.error("no files could be loaded") rl.CloseWindow() return } @@ -79,8 +79,8 @@ main :: proc() { rl.ClearBackground(app.config.colours.background) for file in app.files { - if file.finished_saving && thread.is_done(file.thread) { - thread.destroy(file.thread) + if app.thread_finished && thread.is_done(app.thread) { + thread.destroy(app.thread) } } @@ -263,18 +263,10 @@ main :: proc() { // Handle keyboard shortcuts if rl.IsKeyDown(.LEFT_CONTROL) || rl.IsKeyDown(.RIGHT_CONTROL) { if rl.IsKeyPressed(.S) { - current_file.thread = thread.create_and_start_with_poly_data2(&app, current_file, save_file) + app.thread = thread.create_and_start_with_poly_data2(&app, current_file, save_file) } if rl.IsKeyPressed(.O) { - app.file_dialog_open = true - result := file_dialog.show_open_file_dialog() - app.file_dialog_open = false - if !result.success { - log.error(result.error_message) - } - defer file_dialog.destroy_result(&result) - - open_file(&app, result.file_path) + app.thread = thread.create_and_start_with_poly_data(&app, open_file) } if rl.IsKeyPressed(.A) { select_all(current_file) @@ -333,7 +325,9 @@ load_fonts :: proc(app: ^App) { app.line_height = app.fonts[app.current_font_index].size + 4 } -open_file :: proc(app: ^App, filename: string = "") { +create_file_buffer :: proc(app: ^App, filename: string = "") -> (ok: bool) { + ok = false + buffer: File_Buffer if len(filename) == 0 { buffer = File_Buffer{ @@ -362,6 +356,31 @@ open_file :: proc(app: ^App, filename: string = "") { } append(&app.files, buffer) + + ok = true + + return +} + +open_file :: proc(app: ^App) { + app.file_dialog_open = true + result: file_dialog.Result + result = file_dialog.show_open_file_dialog() + app.file_dialog_open = false + if !result.success { + log.error(result.error_message) + return + } + defer file_dialog.destroy_result(&result) + + ok := create_file_buffer(app, result.file_path) + if !ok { + log.error("failed ot create file buffer") + return + } + app.active_tab+=1 + + return } get_wrapped_lines :: proc(content: string, font: ^FontConfig, max_width: f32) -> []WrappedLine { @@ -866,7 +885,7 @@ move_cursor_to_end_of_line :: proc(file: ^File_Buffer) { } save_file :: proc(app: ^App, file: ^File_Buffer) { - file.finished_saving = false + app.thread_finished = false if len(file.filename) == 0 { app.file_dialog_open = true result := file_dialog.show_save_file_dialog() @@ -885,6 +904,6 @@ save_file :: proc(app: ^App, file: ^File_Buffer) { log.error("failed to save file, \"", file.filename, "\"", sep="") // TODO: Show popup window that displays this to the user return } - file.finished_saving = true + app.thread_finished = true } diff --git a/types.odin b/types.odin index 999bc80..fe767b3 100644 --- a/types.odin +++ b/types.odin @@ -34,9 +34,7 @@ File_Buffer :: struct { modified: bool, cursor_pos: int, scroll_y: f32, - selection: Selection, - thread: ^thread.Thread, - finished_saving: bool + selection: Selection } Colours :: struct { @@ -74,5 +72,7 @@ App :: struct { is_dragging: bool, copied_text: string, config: Config, + thread: ^thread.Thread, + thread_finished: bool, file_dialog_open: bool }