Ders 3
This commit is contained in:
parent
30698d7dd1
commit
e84de621c2
4 changed files with 195 additions and 24 deletions
|
@ -10,7 +10,12 @@ in pkgs.mkShell {
|
||||||
pkgs.gobject-introspection
|
pkgs.gobject-introspection
|
||||||
pkgs.pkg-config
|
pkgs.pkg-config
|
||||||
pkgs.libnotify
|
pkgs.libnotify
|
||||||
# pkgs.libadwaita
|
pkgs.libadwaita
|
||||||
|
# pkgs.xdg-desktop-portal-shana
|
||||||
|
# pkgs.xdg-desktop-portal
|
||||||
|
# pkgs.xdg-desktop-portal-gtk
|
||||||
|
# pkgs.xdg-desktop-portal-gnome
|
||||||
|
# pkgs.gtk3
|
||||||
(pkgs.python3.withPackages (python-pkgs: [
|
(pkgs.python3.withPackages (python-pkgs: [
|
||||||
# select Python packages here
|
# select Python packages here
|
||||||
# python-pkgs.gi
|
# python-pkgs.gi
|
||||||
|
|
39
src/FileOperations.py
Normal file
39
src/FileOperations.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import gi, sys
|
||||||
|
gi.require_version('Gtk', '4.0')
|
||||||
|
from gi.repository import GLib, Gio
|
||||||
|
|
||||||
|
def get_name(file):
|
||||||
|
info = file.query_info("standard::display-name", Gio.FileQueryInfoFlags.NONE)
|
||||||
|
if info:
|
||||||
|
return info.get_attribute_string("standard::display-name")
|
||||||
|
else:
|
||||||
|
return file.get_basename()
|
||||||
|
|
||||||
|
def read_file(file, on_finish_callback):
|
||||||
|
file.load_contents_async(None, _read_file_finish, on_finish_callback)
|
||||||
|
|
||||||
|
def _read_file_finish(file, result, on_finish_callback):
|
||||||
|
contents = file.load_contents_finish(result)
|
||||||
|
|
||||||
|
if not contents[0]:
|
||||||
|
path = file.peek_path()
|
||||||
|
on_finish_callback(file, "", f"Unable to open {path}: {contents[1]}")
|
||||||
|
return;
|
||||||
|
|
||||||
|
try:
|
||||||
|
on_finish_callback(file, contents[1].decode('utf-8'), "")
|
||||||
|
|
||||||
|
except UnicodeError as err:
|
||||||
|
path = file.peek_path()
|
||||||
|
on_finish_callback(file, "", f"Unable to load the contents of {path}: the file is not encoded with UTF-8")
|
||||||
|
|
||||||
|
|
||||||
|
def save_file(file, text, on_finish_callback):
|
||||||
|
file.replace_contents_async(str.encode(text), None, False, Gio.FileCreateFlags.NONE, None, _save_file_finish, on_finish_callback)
|
||||||
|
|
||||||
|
def _save_file_finish(file, result, on_finish_callback):
|
||||||
|
(success, new_etag) = file.replace_contents_finish(result)
|
||||||
|
|
||||||
|
on_finish_callback(file, success, new_etag)
|
||||||
|
|
||||||
|
|
|
@ -2,48 +2,92 @@ import gi, sys
|
||||||
gi.require_version('Gtk', '4.0')
|
gi.require_version('Gtk', '4.0')
|
||||||
from gi.repository import Gtk, Gio
|
from gi.repository import Gtk, Gio
|
||||||
|
|
||||||
|
import FileOperations
|
||||||
|
|
||||||
class MainWindow(Gtk.ApplicationWindow):
|
class MainWindow(Gtk.ApplicationWindow):
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
super().__init__(application=app)
|
super().__init__(application=app)
|
||||||
|
|
||||||
self.setup_actions()
|
self.setup_variables()
|
||||||
|
|
||||||
|
self.setup_actions()
|
||||||
|
|
||||||
self.setup_window()
|
self.setup_window()
|
||||||
|
|
||||||
self.setup_headerbar()
|
self.setup_headerbar()
|
||||||
|
|
||||||
self.setup_ui()
|
self.setup_ui()
|
||||||
|
|
||||||
# == SETUPS ==
|
# == SETUPS ==
|
||||||
|
def setup_variables(self):
|
||||||
|
self._current_working_file = None
|
||||||
|
|
||||||
|
|
||||||
def setup_actions(self):
|
def setup_actions(self):
|
||||||
pass
|
new_action = Gio.SimpleAction(name="new")
|
||||||
|
new_action.connect("activate", self.on_action_win_new_activated)
|
||||||
|
self.add_action(new_action)
|
||||||
|
|
||||||
|
open_action = Gio.SimpleAction(name="open")
|
||||||
|
open_action.connect("activate", self.on_action_win_open_activated)
|
||||||
|
self.add_action(open_action)
|
||||||
|
|
||||||
|
save_action = Gio.SimpleAction(name="save")
|
||||||
|
save_action.connect("activate" , self.on_action_win_save_activated)
|
||||||
|
self.add_action(save_action)
|
||||||
|
|
||||||
|
save_as_action = Gio.SimpleAction(name="save-as")
|
||||||
|
save_as_action.connect("activate", self.on_action_win_save_as_activated)
|
||||||
|
self.add_action(save_as_action)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def setup_window(self):
|
def setup_window(self):
|
||||||
self.set_default_size(600, 400)
|
self.set_default_size(600, 400)
|
||||||
self.set_title("Metin Editörü")
|
self.set_title("Metin Editörü")
|
||||||
|
|
||||||
def setup_headerbar(self):
|
def setup_headerbar(self):
|
||||||
btn_new = Gtk.Button(label="New", icon_name="document-new-symbolic", tooltip_text="Yeni Doküman")
|
btn_new = Gtk.Button(
|
||||||
btn_open = Gtk.Button(label="Open", icon_name="document-open-symbolic", tooltip_text="Dosya Aç")
|
label="New",
|
||||||
btn_save = Gtk.Button(label="Save", icon_name="document-save-symbolic", tooltip_text="Kaydet")
|
action_name="win.new",
|
||||||
btn_save_as = Gtk.Button(label="Save As", icon_name="document-save-as-symbolic", tooltip_text="Farklı Kaydet")
|
icon_name="document-new-symbolic",
|
||||||
|
tooltip_text="Yeni Doküman"
|
||||||
|
)
|
||||||
|
btn_open = Gtk.Button(
|
||||||
|
label="Open",
|
||||||
|
action_name="win.open",
|
||||||
|
icon_name="document-open-symbolic",
|
||||||
|
tooltip_text="Dosya Aç"
|
||||||
|
)
|
||||||
|
btn_save = Gtk.Button(
|
||||||
|
label="Save",
|
||||||
|
action_name="win.save",
|
||||||
|
icon_name="document-save-symbolic",
|
||||||
|
tooltip_text="Kaydet"
|
||||||
|
)
|
||||||
|
btn_save_as = Gtk.Button(
|
||||||
|
label="Save As",
|
||||||
|
action_name="win.save-as",
|
||||||
|
icon_name="document-save-as-symbolic",
|
||||||
|
tooltip_text="Farklı Kaydet"
|
||||||
|
)
|
||||||
|
|
||||||
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
||||||
box.append(btn_new)
|
box.append(btn_new)
|
||||||
box.append(btn_open)
|
box.append(btn_open)
|
||||||
box.append(btn_save)
|
box.append(btn_save)
|
||||||
box.append(btn_save_as)
|
box.append(btn_save_as)
|
||||||
|
|
||||||
headerbar = Gtk.HeaderBar()
|
headerbar = Gtk.HeaderBar()
|
||||||
headerbar.pack_start(box)
|
headerbar.pack_start(box)
|
||||||
|
|
||||||
btn_preferences = Gtk.Button(label="Preferences", icon_name="preferences-system-symbolic", tooltip_text="Seçenekler")
|
btn_preferences = Gtk.Button(label="Preferences", icon_name="open-menu-symbolic", tooltip_text="Seçenekler")
|
||||||
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
||||||
box.append(btn_preferences)
|
box.append(btn_preferences)
|
||||||
headerbar.pack_end(box)
|
headerbar.pack_end(box)
|
||||||
|
|
||||||
self.set_titlebar(headerbar)
|
self.set_titlebar(headerbar)
|
||||||
|
|
||||||
def setup_ui(self):
|
def setup_ui(self):
|
||||||
self.text_view = Gtk.TextView (
|
self.text_view = Gtk.TextView (
|
||||||
monospace=True,
|
monospace=True,
|
||||||
|
@ -54,8 +98,90 @@ class MainWindow(Gtk.ApplicationWindow):
|
||||||
scrolled_window = Gtk.ScrolledWindow(child=self.text_view)
|
scrolled_window = Gtk.ScrolledWindow(child=self.text_view)
|
||||||
|
|
||||||
self.set_child(scrolled_window)
|
self.set_child(scrolled_window)
|
||||||
|
|
||||||
# == FUNCTIONS ==
|
# == FUNCTIONS ==
|
||||||
|
def get_textview_text(self):
|
||||||
|
buffer = self.text_view.get_buffer()
|
||||||
|
|
||||||
|
start = buffer.get_start_iter()
|
||||||
|
|
||||||
|
end = buffer.get_end_iter()
|
||||||
|
return buffer.get_text(start, end, False)
|
||||||
|
|
||||||
|
def set_textview_text(self, text):
|
||||||
|
buffer = self.text_view.get_buffer()
|
||||||
|
|
||||||
|
buffer.set_text(text)
|
||||||
|
|
||||||
|
start = buffer.get_start_iter()
|
||||||
|
buffer.place_cursor(start)
|
||||||
|
|
||||||
|
|
||||||
# == CALLBACKS ==
|
# == CALLBACKS ==
|
||||||
def on_btn_button_clicked(self, btn):
|
def on_action_win_new_activated(self, action, params):
|
||||||
print("Merhaba, evet bu bir buton")
|
self.set_textview_text("")
|
||||||
|
self.set_title("New File")
|
||||||
|
self._current_working_file = None
|
||||||
|
|
||||||
|
def on_action_win_open_activated(self, action, params):
|
||||||
|
self._open_file_chooser = Gtk.FileChooserNative (
|
||||||
|
title="Open File",
|
||||||
|
transient_for=self,
|
||||||
|
action=Gtk.FileChooserAction.OPEN,
|
||||||
|
accept_label="_Open",
|
||||||
|
cancel_label="_Cancel"
|
||||||
|
)
|
||||||
|
|
||||||
|
self._open_file_chooser.connect("response", self.on_open_dialog_response)
|
||||||
|
self._open_file_chooser.show()
|
||||||
|
|
||||||
|
def on_open_dialog_response(self, dialog, response):
|
||||||
|
if response == Gtk.ResponseType.ACCEPT:
|
||||||
|
FileOperations.read_file(dialog.get_file(), self.on_file_read)
|
||||||
|
|
||||||
|
self._open_file_chooser = None
|
||||||
|
|
||||||
|
def on_file_read(self, file, content, err):
|
||||||
|
if err:
|
||||||
|
print(f"Error: {err}")
|
||||||
|
return
|
||||||
|
self.set_textview_text(content)
|
||||||
|
|
||||||
|
filename = FileOperations.get_name(file)
|
||||||
|
self.set_title(filename)
|
||||||
|
self._current_working_file = file
|
||||||
|
|
||||||
|
def on_action_win_save_activated(self, action, params):
|
||||||
|
if self._current_working_file:
|
||||||
|
text = self.get_textview_text()
|
||||||
|
FileOperations.save_file(self._current_working_file, text, self.on_file_saved)
|
||||||
|
else:
|
||||||
|
self.activate_action("win.save-as")
|
||||||
|
|
||||||
|
def on_action_win_save_as_activated(self, action, params):
|
||||||
|
self._save_file_chooser = Gtk.FileChooserNative (
|
||||||
|
title="Save File",
|
||||||
|
transient_for=self,
|
||||||
|
action=Gtk.FileChooserAction.SAVE,
|
||||||
|
accept_label="_Save",
|
||||||
|
cancel_label="_Cancel"
|
||||||
|
)
|
||||||
|
|
||||||
|
self._save_file_chooser.connect("response", self.on_save_dialog_response)
|
||||||
|
self._save_file_chooser.show()
|
||||||
|
|
||||||
|
def on_save_dialog_response(self, dialog, response):
|
||||||
|
if response == Gtk.ResponseType.ACCEPT:
|
||||||
|
text = self.get_textview_text()
|
||||||
|
|
||||||
|
FileOperations.save_file(dialog.get_file(), text, self.on_file_saved)
|
||||||
|
|
||||||
|
self._save_file_chooser = None
|
||||||
|
|
||||||
|
def on_file_saved(self, file, success, new_etag):
|
||||||
|
if success:
|
||||||
|
filename = FileOperations.get_name(file)
|
||||||
|
self.set_title(filename)
|
||||||
|
self._current_working_file = file
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,9 @@ def on_activate(app):
|
||||||
app = Gtk.Application(application_id='com.asandikci.gtk4py')
|
app = Gtk.Application(application_id='com.asandikci.gtk4py')
|
||||||
app.connect('activate', on_activate)
|
app.connect('activate', on_activate)
|
||||||
|
|
||||||
|
app.set_accels_for_action('win.new', ['<Ctrl>t'])
|
||||||
app.set_accels_for_action('win.open', ['<Ctrl>o'])
|
app.set_accels_for_action('win.open', ['<Ctrl>o'])
|
||||||
app.set_accels_for_action('win.save', ['<Ctrl>s'])
|
app.set_accels_for_action('win.save', ['<Ctrl>s'])
|
||||||
app.set_accels_for_action('win.save-as', ['<Ctrl><Shift>s'])
|
app.set_accels_for_action('win.save-as', ['<Ctrl><Shift>s'])
|
||||||
|
|
||||||
app.run(sys.argv)
|
app.run(sys.argv)
|
||||||
|
|
Loading…
Reference in a new issue