Open and image
This commit is contained in:
parent
ac17f705ba
commit
84099d4928
27
src/main.rs
27
src/main.rs
@ -5,6 +5,11 @@ use image::{DynamicImage, ImageBuffer, RgbImage};
|
||||
use gtk::{Button, Box as GtkBox, Orientation};
|
||||
use gtk::glib::Propagation;
|
||||
use gtk::gdk_pixbuf::Pixbuf;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use gtk::gio::File;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
fn main() {
|
||||
// Инициализация приложения
|
||||
@ -30,11 +35,17 @@ fn build_ui(app: &Application) {
|
||||
.build();
|
||||
window.set_resizable(false);
|
||||
|
||||
let selected_file = Rc::new(RefCell::new(None::<File>));
|
||||
|
||||
// Создание кнопки для открытия диалога выбора файла
|
||||
let button = Button::with_label("Select Image");
|
||||
let picture = Picture::new();
|
||||
picture.set_size_request(400, 400);
|
||||
|
||||
{
|
||||
let window = window.clone();
|
||||
let selected_file = selected_file.clone();
|
||||
let picture = picture.clone();
|
||||
button.connect_clicked(move |_| {
|
||||
// Создание FileDialog
|
||||
let dialog = FileDialog::builder()
|
||||
@ -44,12 +55,14 @@ fn build_ui(app: &Application) {
|
||||
|
||||
// Фильтр для изображений
|
||||
let filter = gtk::FileFilter::new();
|
||||
filter.add_mime_type("image/*"); // Разрешить только изображения
|
||||
filter.add_mime_type("image/png"); // Разрешить только изображения
|
||||
filter.set_name(Some("Image Files"));
|
||||
dialog.set_default_filter(Some(&filter));
|
||||
|
||||
// Показ диалога и обработка результата
|
||||
let window = window.clone();
|
||||
let selected_file = selected_file.clone();
|
||||
let picture = picture.clone();
|
||||
dialog.open(Some(&window), None::<>k::gio::Cancellable>, move |result| {
|
||||
if let Ok(file) = result {
|
||||
// Получение выбранного файла
|
||||
@ -59,6 +72,10 @@ fn build_ui(app: &Application) {
|
||||
// Загрузка изображения (опционально)
|
||||
if let Ok(pixbuf) = Pixbuf::from_file(&path) {
|
||||
println!("Image loaded successfully: {}x{}", pixbuf.width(), pixbuf.height());
|
||||
let _ = fs::copy(&path, "/tmp/modified.png");
|
||||
*selected_file.borrow_mut() = Some(file.clone());
|
||||
picture.set_pixbuf(Some(&pixbuf));
|
||||
|
||||
} else {
|
||||
eprintln!("Failed to load image.");
|
||||
}
|
||||
@ -66,7 +83,12 @@ fn build_ui(app: &Application) {
|
||||
eprintln!("File selection canceled.");
|
||||
}
|
||||
});
|
||||
});}
|
||||
});
|
||||
}
|
||||
|
||||
if selected_file.borrow().is_some() {
|
||||
button.set_visible(false);
|
||||
}
|
||||
|
||||
let br_slider = Scale::with_range(Orientation::Horizontal, -128., 128., 1.);
|
||||
br_slider.set_value(0.);
|
||||
@ -91,6 +113,7 @@ fn build_ui(app: &Application) {
|
||||
|
||||
let container = GtkBox::new(Orientation::Vertical, 8);
|
||||
container.append(&button);
|
||||
container.append(&picture);
|
||||
container.append(&br_container);
|
||||
|
||||
window.set_child(Some(&container));
|
||||
|
Loading…
x
Reference in New Issue
Block a user