rollback to gtk 4.8

This commit is contained in:
leca 2024-05-09 02:31:08 +03:00
parent ada58bc37b
commit a2c4dbcb8e
3 changed files with 93 additions and 64 deletions

View File

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] } gtk = { version = "0.8.1", package = "gtk4", features = ["v4_8"] }
rand = "0.8.5" rand = "0.8.5"
rusqlite = "0.31.0" rusqlite = "0.31.0"

View File

@ -1,3 +1,6 @@
// There's no way to pack an appinage with gtk 4.12 as there's no way to put glibc inside an appimage according to https://github.com/AppImageCommunity/pkg2appimage/blob/master/excludelist
// The only way to do that is rollback to gtk 4.8 and rewrite neccesary parts of the program.
#![windows_subsystem = "windows"] #![windows_subsystem = "windows"]
mod db; mod db;

View File

@ -4,25 +4,26 @@ use std::fs;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::Path; use std::path::{Path, PathBuf};
use std::rc::Rc; use std::rc::Rc;
use crate::card::Card; use crate::card::Card;
use crate::db::*; use crate::db::*;
use crate::ui::cards::new::*; use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry; use crate::widgets::card_entry::CardEntry;
use glib::subclass::InitializingObject; use glib::subclass::InitializingObject;
use gtk::ffi::GtkFileChooserDialog;
use gtk::gio::ListStore; use gtk::gio::ListStore;
use gtk::glib::closure::IntoClosureReturnValue;
use gtk::glib::object::ObjectExt; use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local, Object}; use gtk::glib::{clone, closure_local, Object};
use gtk::prelude::WidgetExt; use gtk::prelude::WidgetExt;
use gtk::prelude::*;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use gtk::{ use gtk::{
gio, glib, Button, CompositeTemplate, ListView, NoSelection, ScrolledWindow, SearchEntry, gio, glib, ApplicationWindow, Button, CompositeTemplate, FileChooserAction, ListView,
Switch, Window, NoSelection, ResponseType, ScrolledWindow, SearchEntry, Switch, Window,
}; };
use gtk::{prelude::*, FileDialog};
use gtk::{ListItem, SignalListItemFactory}; use gtk::{ListItem, SignalListItemFactory};
use rusqlite::Connection; use rusqlite::Connection;
use sha256::try_digest; use sha256::try_digest;
@ -217,7 +218,6 @@ impl MemoryCardsEditScene {
} }
pub fn query_cards(&self, options: Option<String>) { pub fn query_cards(&self, options: Option<String>) {
let conn = Connection::open(get_db_path()).unwrap(); let conn = Connection::open(get_db_path()).unwrap();
let selector = match options { let selector = match options {
@ -229,29 +229,33 @@ impl MemoryCardsEditScene {
"SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}" "SELECT imagename, hieroglyph, reading, translation, is_learning FROM cards {selector}"
); );
let mut stmt = conn.prepare(sql.as_str()).unwrap(); let mut stmt = conn.prepare(sql.as_str()).unwrap();
let cards_iter = stmt let cards_iter = stmt
.query_map([], |row| { .query_map([], |row| {
let image_path: String = match row.get(0) { let image_path: String = match row.get(0) {
Ok(path) => path, Ok(path) => path,
Err(_) => String::from(""), Err(_) => String::from(""),
}; };
let c = Card::new( let c = Card::new(
Some(get_images_store_path() + &image_path), Some(get_images_store_path() + &image_path),
Some(row.get(1).unwrap()), Some(row.get(1).unwrap()),
Some(row.get(2).unwrap()), Some(row.get(2).unwrap()),
Some(row.get(3).unwrap()), Some(row.get(3).unwrap()),
Some(row.get(4).unwrap()), Some(row.get(4).unwrap()),
); );
let entry = CardEntry::new(Some(&c)); let entry = CardEntry::new(Some(&c));
entry.update_state(); entry.update_state();
Ok(entry) Ok(entry)
}) })
.unwrap(); .unwrap();
self.displaying_cards.lock().unwrap().borrow_mut().clear(); self.displaying_cards.lock().unwrap().borrow_mut().clear();
for c in cards_iter { for c in cards_iter {
self.displaying_cards.lock().unwrap().borrow_mut().push(c.unwrap()); self.displaying_cards
.lock()
.unwrap()
.borrow_mut()
.push(c.unwrap());
} }
} }
} }
@ -262,46 +266,68 @@ async fn new_card_setup<W: IsA<gtk::Window>>(window: Rc<W>) {
.unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene .unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
let picture_widget = w.get_picture_widget(); let picture_widget = w.get_picture_widget();
let dialog = gtk::FileChooserDialog::new(
let dialog: FileDialog = gtk::FileDialog::builder().build(); Some("Open File"),
let answer = dialog.open_future(Some(&*window)).await; Some(Into::<&Window>::into(w.upcast_ref())),
let path = match answer { FileChooserAction::Open,
Ok(p) => p, &[
Err(_) => return, ("_Cancel", ResponseType::Cancel),
} ("_Open", ResponseType::Accept),
.path() ],
.unwrap();
let path: String = path.as_path().to_str().unwrap().to_owned();
picture_widget.set_file(Some(&gio::File::for_path(&path)));
let images_store_path = get_program_home_path() + "/images";
match fs::create_dir_all(&images_store_path) {
Ok(_) => {}
Err(error) => match error.kind() {
ErrorKind::AlreadyExists => {}
_ => panic!("Could not create directory for storing images!"),
},
};
w.get_done_button().connect_closure(
"clicked",
false,
closure_local!(@strong w, @strong path => move |_b: &Button| {
let hash = try_digest(&path).unwrap();
let extenstion = Path::new(path.as_str()).extension().unwrap().to_str().unwrap();
let new_filename: String = hash.as_str().to_owned() + "." + extenstion;
let stored_image_path = Path::new(&images_store_path).join(&new_filename);
fs::copy(&path, stored_image_path).expect("Error copying image to store");
let hieroglyph = w.get_hieroglyph_input();
let conn = Connection::open(get_db_path()).unwrap();
conn.execute("UPDATE cards SET imagename = ?1 WHERE hieroglyph = ?2", (&new_filename, &hieroglyph)).unwrap();
conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap();
}),
); );
let path = RefCell::new(String::from(""));
dialog.run_async(clone!( @strong path, @strong w, @strong picture_widget => move |a, _b| {
let c = a.file().unwrap().path().unwrap().as_os_str().to_owned().to_str().unwrap().to_string();
println!("{c}");
*path.borrow_mut() = c;
a.close();
picture_widget.set_file(Some(&gio::File::for_path(Path::new(path.borrow().as_str()))));
let images_store_path = get_program_home_path() + "/images";
match fs::create_dir_all(&images_store_path) {
Ok(_) => {}
Err(error) => match error.kind() {
ErrorKind::AlreadyExists => {}
_ => panic!("Could not create directory for storing images!"),
},
};
w.get_done_button().connect_closure(
"clicked",
false,
closure_local!(@strong w, @strong path => move |_b: &Button| {
let p = path.borrow();
let p = p.as_str();
let hash = try_digest(path.borrow().as_str()).unwrap();
let extenstion = Path::new(p).extension().unwrap().to_str().unwrap();
let new_filename: String = hash.as_str().to_owned() + "." + extenstion;
let stored_image_path = Path::new(&images_store_path).join(&new_filename);
fs::copy(&p, stored_image_path).expect("Error copying image to store");
let hieroglyph = w.get_hieroglyph_input();
let conn = Connection::open(get_db_path()).unwrap();
conn.execute("UPDATE cards SET imagename = ?1 WHERE hieroglyph = ?2", (&new_filename, &hieroglyph)).unwrap();
conn.execute("UPDATE cards SET is_learning = TRUE WHERE hieroglyph = ?1", [&hieroglyph]).unwrap();
}),
);
}));
// let dialog: FileDialog = gtk::FileDialog::builder().build();
// let answer = Ok("");
// let answer = dialog.open_future(Some(&*window)).await;
// let path = match answer {
// Ok(p) => p,
// Err(_) => return,
// };
// .path()
// .unwrap();
// let path: String = path.as_path().to_str().unwrap().to_owned();
} }
impl WidgetImpl for MemoryCardsEditScene {} impl WidgetImpl for MemoryCardsEditScene {}