Skip to content

Commit

Permalink
Merge pull request #23 from hatal175/master
Browse files Browse the repository at this point in the history
Add set_title to FileDialog
  • Loading branch information
PolyMeilex authored Sep 12, 2021
2 parents c1855df + ac7fe4d commit da92e0c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/backend/gtk3/file_dialog/dialog_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl GtkFileDialog {
impl GtkFileDialog {
pub fn build_pick_file(opt: &FileDialog) -> Self {
let mut dialog =
GtkFileDialog::new("Open File", GtkFileChooserAction::Open, "Cancel", "Open");
GtkFileDialog::new(opt.title.as_deref().unwrap_or("Open File"), GtkFileChooserAction::Open, "Cancel", "Open");

dialog.add_filters(&opt.filters);
dialog.set_path(opt.starting_directory.as_deref());
Expand All @@ -166,7 +166,7 @@ impl GtkFileDialog {

pub fn build_save_file(opt: &FileDialog) -> Self {
let mut dialog =
GtkFileDialog::new("Save File", GtkFileChooserAction::Save, "Cancel", "Save");
GtkFileDialog::new(opt.title.as_deref().unwrap_or("Save File"), GtkFileChooserAction::Save, "Cancel", "Save");

unsafe { gtk_sys::gtk_file_chooser_set_do_overwrite_confirmation(dialog.ptr as _, 1) };

Expand Down Expand Up @@ -194,7 +194,7 @@ impl GtkFileDialog {

pub fn build_pick_folder(opt: &FileDialog) -> Self {
let dialog = GtkFileDialog::new(
"Select Folder",
opt.title.as_deref().unwrap_or("Select Folder"),
GtkFileChooserAction::SelectFolder,
"Cancel",
"Select",
Expand All @@ -206,7 +206,7 @@ impl GtkFileDialog {

pub fn build_pick_files(opt: &FileDialog) -> Self {
let mut dialog =
GtkFileDialog::new("Open File", GtkFileChooserAction::Open, "Cancel", "Open");
GtkFileDialog::new(opt.title.as_deref().unwrap_or("Open File"), GtkFileChooserAction::Open, "Cancel", "Open");

unsafe { gtk_sys::gtk_file_chooser_set_select_multiple(dialog.ptr as _, 1) };
dialog.add_filters(&opt.filters);
Expand Down
23 changes: 23 additions & 0 deletions src/backend/macos/file_dialog/panel_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ impl Panel {
}
}

pub fn set_title(&self, title: &str) {
unsafe {
let title = make_nsstring(title);
let () = msg_send![self.panel, setTitle: title];
}
}

pub fn get_result(&self) -> PathBuf {
unsafe {
let url = msg_send![self.panel, URL];
Expand Down Expand Up @@ -155,6 +162,10 @@ impl Panel {
panel.set_file_name(file_name);
}

if let Some(title) = &opt.title {
panel.set_title(title);
}

panel.set_can_choose_directories(NO);
panel.set_can_choose_files(YES);

Expand All @@ -176,6 +187,10 @@ impl Panel {
panel.set_file_name(file_name);
}

if let Some(title) = &opt.title {
panel.set_title(title);
}

panel
}

Expand All @@ -186,6 +201,10 @@ impl Panel {
panel.set_path(path, opt.file_name.as_deref());
}

if let Some(title) = &opt.title {
panel.set_title(title);
}

panel.set_can_choose_directories(YES);
panel.set_can_choose_files(NO);

Expand All @@ -203,6 +222,10 @@ impl Panel {
panel.set_path(path, opt.file_name.as_deref());
}

if let Some(title) = &opt.title {
panel.set_title(title);
}

panel.set_can_choose_directories(NO);
panel.set_can_choose_files(YES);
panel.set_allows_multiple_selection(YES);
Expand Down
15 changes: 15 additions & 0 deletions src/backend/win_cid/file_dialog/dialog_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ impl IDialog {
Ok(())
}

fn set_title(&self, title: &Option<String>) -> Result<(), HRESULT> {
if let Some(title) = title {
let wide_title: Vec<u16> = OsStr::new(title).encode_wide().chain(once(0)).collect();

unsafe {
(*self.0).SetTitle(wide_title.as_ptr()).check()?;
}
}
Ok(())
}

pub fn get_results(&self) -> Result<Vec<PathBuf>, HRESULT> {
unsafe {
let mut res_items: *mut IShellItemArray = ptr::null_mut();
Expand Down Expand Up @@ -237,6 +248,7 @@ impl IDialog {
dialog.add_filters(&opt.filters)?;
dialog.set_path(&opt.starting_directory)?;
dialog.set_file_name(&opt.file_name)?;
dialog.set_title(&opt.title)?;

Ok(dialog)
}
Expand All @@ -247,6 +259,7 @@ impl IDialog {
dialog.add_filters(&opt.filters)?;
dialog.set_path(&opt.starting_directory)?;
dialog.set_file_name(&opt.file_name)?;
dialog.set_title(&opt.title)?;

Ok(dialog)
}
Expand All @@ -255,6 +268,7 @@ impl IDialog {
let dialog = IDialog::new_open_dialog(opt)?;

dialog.set_path(&opt.starting_directory)?;
dialog.set_title(&opt.title)?;

unsafe {
dialog.SetOptions(FOS_PICKFOLDERS).check()?;
Expand All @@ -269,6 +283,7 @@ impl IDialog {
dialog.add_filters(&opt.filters)?;
dialog.set_path(&opt.starting_directory)?;
dialog.set_file_name(&opt.file_name)?;
dialog.set_title(&opt.title)?;

unsafe {
dialog.SetOptions(FOS_ALLOWMULTISELECT).check()?;
Expand Down
21 changes: 21 additions & 0 deletions src/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct FileDialog {
pub(crate) filters: Vec<Filter>,
pub(crate) starting_directory: Option<PathBuf>,
pub(crate) file_name: Option<String>,
pub(crate) title: Option<String>,
#[cfg(feature = "parent")]
pub(crate) parent: Option<RawWindowHandle>,
}
Expand Down Expand Up @@ -73,6 +74,16 @@ impl FileDialog {
self
}

/// Set the title of the dialog.
/// #### Supported Platforms:
/// - Windows
/// - Linux
/// - Mac (Only below version 10.11)
pub fn set_title(mut self, title: &str) -> Self {
self.title = Some(title.into());
self
}

#[cfg(feature = "parent")]
/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
Expand Down Expand Up @@ -172,6 +183,16 @@ impl AsyncFileDialog {
self
}

/// Set the title of the dialog.
/// #### Supported Platforms:
/// - Windows
/// - Linux
/// - Mac (Only below version 10.11)
pub fn set_title(mut self, title: &str) -> Self {
self.file_dialog = self.file_dialog.set_title(title);
self
}

#[cfg(feature = "parent")]
/// Set parent windows explicitly (optional)
/// Suported in: `macos` and `windows`
Expand Down

0 comments on commit da92e0c

Please sign in to comment.