-
Notifications
You must be signed in to change notification settings - Fork 569
Add file filtering to gtk dialogs. #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright 2020 The xi-editor Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use druid::widget::{Align, Button, Flex, TextBox}; | ||
| use druid::{ | ||
| AppDelegate, AppLauncher, Command, DelegateCtx, Env, FileDialogOptions, FileInfo, FileSpec, | ||
| LocalizedString, Target, Widget, WindowDesc, | ||
| }; | ||
|
|
||
| struct Delegate; | ||
|
|
||
| pub fn main() { | ||
| let main_window = WindowDesc::new(ui_builder) | ||
| .title(LocalizedString::new("open-save-demo").with_placeholder("Opening/Saving Demo")); | ||
| let data = "Type here.".to_owned(); | ||
| AppLauncher::with_window(main_window) | ||
| .delegate(Delegate) | ||
| .use_simple_logger() | ||
| .launch(data) | ||
| .expect("launch failed"); | ||
| } | ||
|
|
||
| fn ui_builder() -> impl Widget<String> { | ||
| let rs = FileSpec::new("Rust source", &["rs"]); | ||
| let txt = FileSpec::new("Text file", &["txt"]); | ||
| let other = FileSpec::new("Bogus file", &["foo", "bar", "baz"]); | ||
| let save_dialog_options = FileDialogOptions::new() | ||
| .allowed_types(vec![rs, txt, other]) | ||
| .default_type(txt); | ||
| let open_dialog_options = save_dialog_options.clone(); | ||
|
|
||
| let input = TextBox::new(); | ||
| let save = Button::new("Save").on_click(move |ctx, _, _| { | ||
| ctx.submit_command( | ||
| Command::new( | ||
| druid::commands::SHOW_SAVE_PANEL, | ||
| save_dialog_options.clone(), | ||
| ), | ||
| None, | ||
| ) | ||
| }); | ||
| let open = Button::new("Open").on_click(move |ctx, _, _| { | ||
| ctx.submit_command( | ||
| Command::new( | ||
| druid::commands::SHOW_OPEN_PANEL, | ||
| open_dialog_options.clone(), | ||
| ), | ||
| None, | ||
| ) | ||
| }); | ||
|
|
||
| let mut col = Flex::column(); | ||
| col.add_child(input); | ||
| col.add_spacer(8.0); | ||
| col.add_child(save); | ||
| col.add_child(open); | ||
| Align::centered(col) | ||
| } | ||
|
|
||
| impl AppDelegate<String> for Delegate { | ||
| fn command( | ||
| &mut self, | ||
| _ctx: &mut DelegateCtx, | ||
| _target: Target, | ||
| cmd: &Command, | ||
| data: &mut String, | ||
| _env: &Env, | ||
| ) -> bool { | ||
| match cmd.selector { | ||
| druid::commands::SAVE_FILE => { | ||
| if let Ok(file_info) = cmd.get_object::<FileInfo>() { | ||
| if let Err(e) = std::fs::write(file_info.path(), &data[..]) { | ||
| println!("Error writing file: {}", e); | ||
| } | ||
| } | ||
| true | ||
| } | ||
| druid::commands::OPEN_FILE => { | ||
| if let Ok(file_info) = cmd.get_object::<FileInfo>() { | ||
| match std::fs::read_to_string(file_info.path()) { | ||
| Ok(s) => { | ||
| let first_line = s.lines().next().unwrap_or(""); | ||
| *data = first_line.to_owned(); | ||
| } | ||
| Err(e) => { | ||
| println!("Error opening file: {}", e); | ||
| } | ||
| } | ||
| } | ||
| true | ||
| } | ||
| _ => false, | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This warning should be removed, because there is an explictly documented behavior for this case.
If it's
Noneor not present inallowed_typesthen the first entry inallowed_typeswill be used as default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it seems unreasonable for the user to provide a default that is not even available.
If this happens, it most definitely seems to be an accident and should log a warning (on all platforms), at least I think so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that's reasonable. We can keep the warning then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that (and I can change this if it isn't desired druid behavior), if allowed_types is empty then the default type is allowed to be present: it applies a filter that cannot be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be clear, my previous comment is about gtk's behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if I interpret that code correctly, on Windows
Some(default_type)is ignored ifallowed_typesisNone. On mac the default type seems to be ignored all together.I'm not sure what the 'correct' behavior would be (at least not the mac one).
Maybe we should allow having default be
Somewhile allowed isNone?