mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 18:42:43 +00:00 
			
		
		
		
	LibGUI: Add parent window argument to FilePicker functions
Since FilePicker almost always should be modal, add the parent window as mandatory first argument.
This commit is contained in:
		
							parent
							
								
									9844088964
								
							
						
					
					
						commit
						6568765e8f
					
				
					 14 changed files with 27 additions and 26 deletions
				
			
		|  | @ -152,7 +152,7 @@ void DisplaySettingsWidget::create_frame() | |||
|     button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); | ||||
|     button.set_preferred_size(22, 22); | ||||
|     button.on_click = [this](auto) { | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath("Select wallpaper from file system."); | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(root_widget()->window(), "Select wallpaper from file system."); | ||||
| 
 | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
|  |  | |||
|  | @ -54,6 +54,7 @@ int main(int argc, char** argv) | |||
|     auto instance = DisplaySettingsWidget::construct(); | ||||
| 
 | ||||
|     auto window = GUI::Window::construct(); | ||||
|     dbg() << "main window: " << window; | ||||
|     window->set_title("Display settings"); | ||||
|     window->move_to(100, 100); | ||||
|     window->resize(360, 390); | ||||
|  |  | |||
|  | @ -92,7 +92,7 @@ int main(int argc, char** argv) | |||
| 
 | ||||
|     auto& app_menu = menubar->add_menu("Font Editor"); | ||||
|     app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(); | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(window); | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
| 
 | ||||
|  | @ -112,7 +112,7 @@ int main(int argc, char** argv) | |||
|     app_menu.add_action(GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) { | ||||
|         FontEditorWidget* editor = static_cast<FontEditorWidget*>(window->main_widget()); | ||||
|         LexicalPath lexical_path(editor->path()); | ||||
|         Optional<String> save_path = GUI::FilePicker::get_save_filepath(lexical_path.title(), lexical_path.extension()); | ||||
|         Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, lexical_path.title(), lexical_path.extension()); | ||||
|         if (!save_path.has_value()) | ||||
|             return; | ||||
| 
 | ||||
|  |  | |||
|  | @ -95,7 +95,7 @@ HexEditorWidget::HexEditorWidget() | |||
|     }); | ||||
| 
 | ||||
|     m_open_action = GUI::CommonActions::make_open_action([this](auto&) { | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(); | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(window()); | ||||
| 
 | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
|  | @ -118,7 +118,7 @@ HexEditorWidget::HexEditorWidget() | |||
|     }); | ||||
| 
 | ||||
|     m_save_as_action = GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GUI::Action&) { | ||||
|         Optional<String> save_path = GUI::FilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "bin" : m_extension); | ||||
|         Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "bin" : m_extension); | ||||
|         if (!save_path.has_value()) | ||||
|             return; | ||||
| 
 | ||||
|  |  | |||
|  | @ -64,7 +64,7 @@ int main(int argc, char** argv) | |||
|     // Actions
 | ||||
|     auto open_action = GUI::CommonActions::make_open_action( | ||||
|         [&](auto&) { | ||||
|             Optional<String> path = GUI::FilePicker::get_open_filepath("Open"); | ||||
|             Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open"); | ||||
|             if (path.has_value()) { | ||||
|                 keyboard_mapper_widget->load_from_file(path.value()); | ||||
|             } | ||||
|  | @ -78,7 +78,7 @@ int main(int argc, char** argv) | |||
|     auto save_as_action = GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), | ||||
|         [&](auto&) { | ||||
|             String m_name = "Unnamed"; | ||||
|             Optional<String> save_path = GUI::FilePicker::get_save_filepath(m_name, "json"); | ||||
|             Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, m_name, "json"); | ||||
|             if (!save_path.has_value()) | ||||
|                 return; | ||||
| 
 | ||||
|  |  | |||
|  | @ -108,7 +108,7 @@ SamplerWidget::SamplerWidget(TrackManager& track_manager) | |||
|     m_open_button->set_focusable(false); | ||||
|     m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png")); | ||||
|     m_open_button->on_click = [this](auto) { | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(); | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(window()); | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
|         String error_string = m_track_manager.current_track().set_recorded_sample(open_path.value()); | ||||
|  |  | |||
|  | @ -96,7 +96,7 @@ int main(int argc, char** argv) | |||
| 
 | ||||
|     auto& app_menu = menubar->add_menu("Piano"); | ||||
|     app_menu.add_action(GUI::Action::create("Export", { Mod_Ctrl, Key_E }, [&](const GUI::Action&) { | ||||
|         save_path = GUI::FilePicker::get_save_filepath("Untitled", "wav"); | ||||
|         save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "wav"); | ||||
|         if (!save_path.has_value()) | ||||
|             return; | ||||
|         wav_writer.set_file(save_path.value()); | ||||
|  |  | |||
|  | @ -100,7 +100,7 @@ int main(int argc, char** argv) | |||
|     auto& app_menu = menubar->add_menu("PixelPaint"); | ||||
| 
 | ||||
|     app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(); | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(window); | ||||
| 
 | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
|  |  | |||
|  | @ -126,7 +126,7 @@ int main(int argc, char** argv) | |||
|     // Actions
 | ||||
|     auto open_action = GUI::CommonActions::make_open_action( | ||||
|         [&](auto&) { | ||||
|             Optional<String> path = GUI::FilePicker::get_open_filepath("Open image..."); | ||||
|             Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open image..."); | ||||
|             if (path.has_value()) { | ||||
|                 widget.load_from_file(path.value()); | ||||
|             } | ||||
|  |  | |||
|  | @ -79,7 +79,7 @@ int main(int argc, char** argv) | |||
|     }); | ||||
| 
 | ||||
|     app_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { | ||||
|         Optional<String> path = GUI::FilePicker::get_open_filepath("Open wav file..."); | ||||
|         Optional<String> path = GUI::FilePicker::get_open_filepath(window, "Open wav file..."); | ||||
|         if (path.has_value()) { | ||||
|             player.open_file(path.value()); | ||||
|         } | ||||
|  |  | |||
|  | @ -304,7 +304,7 @@ TextEditorWidget::TextEditorWidget() | |||
|     }); | ||||
| 
 | ||||
|     m_open_action = GUI::CommonActions::make_open_action([this](auto&) { | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(); | ||||
|         Optional<String> open_path = GUI::FilePicker::get_open_filepath(window()); | ||||
| 
 | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
|  | @ -321,7 +321,7 @@ TextEditorWidget::TextEditorWidget() | |||
|     }); | ||||
| 
 | ||||
|     m_save_as_action = GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GUI::Action&) { | ||||
|         Optional<String> save_path = GUI::FilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension); | ||||
|         Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension); | ||||
|         if (!save_path.has_value()) | ||||
|             return; | ||||
| 
 | ||||
|  |  | |||
|  | @ -222,7 +222,7 @@ int main(int argc, char** argv) | |||
|     }); | ||||
| 
 | ||||
|     auto add_existing_file_action = GUI::Action::create("Add existing file to project...", Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) { | ||||
|         auto result = GUI::FilePicker::get_open_filepath("Add existing file to project"); | ||||
|         auto result = GUI::FilePicker::get_open_filepath(g_window, "Add existing file to project"); | ||||
|         if (!result.has_value()) | ||||
|             return; | ||||
|         auto& filename = result.value(); | ||||
|  | @ -425,7 +425,7 @@ int main(int argc, char** argv) | |||
|     }); | ||||
| 
 | ||||
|     auto open_action = GUI::Action::create("Open project...", { Mod_Ctrl | Mod_Shift, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), [&](auto&) { | ||||
|         auto open_path = GUI::FilePicker::get_open_filepath("Open project"); | ||||
|         auto open_path = GUI::FilePicker::get_open_filepath(g_window, "Open project"); | ||||
|         if (!open_path.has_value()) | ||||
|             return; | ||||
|         open_project(open_path.value()); | ||||
|  |  | |||
|  | @ -42,9 +42,9 @@ | |||
| 
 | ||||
| namespace GUI { | ||||
| 
 | ||||
| Optional<String> FilePicker::get_open_filepath(const String& window_title, Options options) | ||||
| Optional<String> FilePicker::get_open_filepath(Window* parent_window, const String& window_title, Options options) | ||||
| { | ||||
|     auto picker = FilePicker::construct(Mode::Open, options); | ||||
|     auto picker = FilePicker::construct(parent_window, Mode::Open, options); | ||||
| 
 | ||||
|     if (!window_title.is_null()) | ||||
|         picker->set_title(window_title); | ||||
|  | @ -60,9 +60,9 @@ Optional<String> FilePicker::get_open_filepath(const String& window_title, Optio | |||
|     return {}; | ||||
| } | ||||
| 
 | ||||
| Optional<String> FilePicker::get_save_filepath(const String& title, const String& extension, Options options) | ||||
| Optional<String> FilePicker::get_save_filepath(Window* parent_window, const String& title, const String& extension, Options options) | ||||
| { | ||||
|     auto picker = FilePicker::construct(Mode::Save, options, String::format("%s.%s", title.characters(), extension.characters())); | ||||
|     auto picker = FilePicker::construct(parent_window, Mode::Save, options, String::format("%s.%s", title.characters(), extension.characters())); | ||||
| 
 | ||||
|     if (picker->exec() == Dialog::ExecOK) { | ||||
|         String file_path = picker->selected_file().string(); | ||||
|  | @ -75,7 +75,7 @@ Optional<String> FilePicker::get_save_filepath(const String& title, const String | |||
|     return {}; | ||||
| } | ||||
| 
 | ||||
| FilePicker::FilePicker(Mode mode, Options options, const StringView& file_name, const StringView& path, Window* parent_window) | ||||
| FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const StringView& file_name, const StringView& path) | ||||
|     : Dialog(parent_window) | ||||
|     , m_model(FileSystemModel::create()) | ||||
|     , m_mode(mode) | ||||
|  |  | |||
|  | @ -49,12 +49,12 @@ public: | |||
|         DisablePreview = (1 << 0) | ||||
|     }; | ||||
| 
 | ||||
|     static Optional<String> get_open_filepath(Options options) | ||||
|     static Optional<String> get_open_filepath(Window* parent_window, Options options) | ||||
|     { | ||||
|         return get_open_filepath({}, options); | ||||
|         return get_open_filepath(parent_window, {}, options); | ||||
|     } | ||||
|     static Optional<String> get_open_filepath(const String& window_title = {}, Options options = Options::None); | ||||
|     static Optional<String> get_save_filepath(const String& title, const String& extension, Options options = Options::None); | ||||
|     static Optional<String> get_open_filepath(Window* parent_window, const String& window_title = {}, Options options = Options::None); | ||||
|     static Optional<String> get_save_filepath(Window* parent_window, const String& title, const String& extension, Options options = Options::None); | ||||
|     static bool file_exists(const StringView& path); | ||||
| 
 | ||||
|     virtual ~FilePicker() override; | ||||
|  | @ -69,7 +69,7 @@ private: | |||
| 
 | ||||
|     virtual void on_model_update(unsigned) override; | ||||
| 
 | ||||
|     FilePicker(Mode type = Mode::Open, Options = Options::None, const StringView& file_name = "Untitled", const StringView& path = Core::StandardPaths::home_directory(), Window* parent_window = nullptr); | ||||
|     FilePicker(Window* parent_window, Mode type = Mode::Open, Options = Options::None, const StringView& file_name = "Untitled", const StringView& path = Core::StandardPaths::home_directory()); | ||||
| 
 | ||||
|     static String ok_button_name(Mode mode) | ||||
|     { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tom
						Tom