Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
the-byte-bender committed Sep 25, 2022
2 parents e6d6f44 + 5a1411d commit c6a910f
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 14 deletions.
7 changes: 6 additions & 1 deletion libs/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ def submit(
threadpool = pools[kwargs.get("threadpool", 0)]
if "threadpool" in kwargs:
del kwargs["threadpool"]
future: Future = threadpool.submit(func, *args, **kwargs)
def wrapped_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
future: Future = threadpool.submit(wrapped_func, *args, **kwargs)
if callback:
if callable(callback):
future.add_done_callback(wrap(callback))
Expand Down
44 changes: 36 additions & 8 deletions libs/ui/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import contextlib
from gi.repository import Gtk
import praw
import praw.util.token_manager
from . import authorize, submitions_list
from .utils import LabeledTextbox
from .utils import LabeledTextbox, InputDialog, SimpleButton
from .. import refresh_token, threadpool


Expand Down Expand Up @@ -41,13 +42,40 @@ def __init__(
return
self.main_notebook: Gtk.Notebook = Gtk.Notebook()
self.main_notebook.get_accessible().set_name("Main tab bar")
self.front_page: submitions_list.SubmitionsList = (
submitions_list.SubmitionsList(
"Front page", self.reddit.front.new(limit=None), True
)
self.front_page: submitions_list.SubmitionsList = submitions_list.SubmitionsList(
"Front page", self.reddit.front.new(limit=None), True # type: ignore
)
self.add_tab("Front page", self.front_page)
self.add(self.main_notebook)
self.add_tab("Front page", self.front_page, False)
self.box = Gtk.Box(Gtk.Orientation.HORIZONTAL)
self.box.add(self.main_notebook)
self.box.add(SimpleButton("New _tab", self.new_tab_dialog))
self.box.add(SimpleButton("_Close current tab", self.remove_current_tab))
self.add(self.box)

def add_tab(self, label: str, widget: Gtk.Widget):
def add_tab(self, label: str, widget: Gtk.Widget, closable: bool = True):
widget._tab_closable = closable
self.main_notebook.append_page(widget, Gtk.Label(label))
self.main_notebook.show_all()
page = self.main_notebook.get_n_pages() - 1
self.main_notebook.set_current_page(page)

def remove_current_tab(self):
page_index = self.main_notebook.get_current_page()
page = self.main_notebook.get_nth_page(page_index)
if page._tab_closable:
self.main_notebook.remove_page(page_index)
self.main_notebook.set_current_page(page_index - 1)
self.main_notebook.show_all()

def new_tab_dialog(self):
if subreddit_name := InputDialog("Please type in a subreddit", "r/").run():
with contextlib.suppress(Exception):
subreddit = self.reddit.subreddit(subreddit_name)
subreddit._fetch()
self.add_subreddit_as_tab(subreddit)

def add_subreddit_as_tab(self, subreddit):
subreddit_widget = submitions_list.SubmitionsList(
subreddit.display_name, subreddit.new(limit=None), False
)
self.add_tab(subreddit.display_name, subreddit_widget)
51 changes: 51 additions & 0 deletions libs/ui/reply_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import typing
from gi.repository import Gtk
from .utils import SimpleButton, LabeledTextbox
from .. import threadpool
import praw.models

ID_SEND = 100
ID_CANCEL = 101


class ReplyDialog(Gtk.Dialog):
def __init__(
self,
parent_window: Gtk.Widget,
title: str,
parent: typing.Union[praw.models.Comment, praw.models.Submission],
):
super().__init__(title)
self.set_transient_for(parent_window)
self.parent = parent
self.box = self.get_content_area()
self.parent_text = (
parent.body
if isinstance(parent, praw.models.Comment)
else (parent.selftext or parent.url or "")
)
self.parent_body = LabeledTextbox("Replying to", self.parent_text, False, True)
self.box.add(self.parent_body)
self.reply_body = LabeledTextbox("Your reply", "", True, True)
self.box.add(self.reply_body)
self.add_button("Send", ID_SEND)
self.add_button("Cancel", ID_CANCEL)

def run(self):
self.show_all()
response = super().run()
if response == ID_SEND:
try:
comment = self.parent.reply(body=self.reply_body.get_text())
if isinstance(self.parent, praw.models.Submission):
self.parent._fetch()
else:
self.parent.refresh()
self.destroy()
return comment
except Exception as e:
print(e)
self.destroy()
return None
self.destroy()
return None
42 changes: 37 additions & 5 deletions libs/ui/submission_dialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import typing
from gi.repository import Gtk
import praw.models, praw.models.comment_forest
from .utils import LabeledTextbox
from .utils import LabeledTextbox, SimpleButton
from .reply_dialog import ReplyDialog
from .. import threadpool


Expand All @@ -27,6 +28,7 @@ def __init__(self, parrent: Gtk.Window, submission: praw.models.Submission):
self.comment_body = LabeledTextbox("Comment body", "", False, True)
self.box.add(self.comment_body)
self.selection = self.comments_tree.get_selection()
self.selection.set_mode(Gtk.SelectionMode.BROWSE)
self.selection.connect("changed", self.on_selection_change)
self.text_renderer = Gtk.CellRendererText()
self.upvote_toggle = Gtk.CellRendererToggle()
Expand All @@ -46,7 +48,13 @@ def __init__(self, parrent: Gtk.Window, submission: praw.models.Submission):
]
for column in self.columns:
self.comments_tree.append_column(column)
self.add_comments(None, submission.comments)
comments = list(submission.comments) # type: ignore
comments.extend(submission._extra_replies)
self.add_comments(None, comments) # type: ignore
self.add_action_widget(SimpleButton("_Comment...", self.comment), 0)
self.add_action_widget(
SimpleButton("_Reply to focused comment...", self.reply), 0
)
self.add_button("Close", Gtk.ResponseType.CLOSE)

def add_comments(
Expand All @@ -58,8 +66,12 @@ def add_comments(
],
collapse_all: bool = True,
):
for comment in comments:
for index, comment in enumerate(comments): # type: ignore
if isinstance(comment, praw.models.Comment):
if not hasattr(comment, "_extra_replies"):
comment._extra_replies = []
if comment in comments[:index]: # type: ignore
continue
position = self.comments_store.append(
parrent,
[
Expand All @@ -75,6 +87,7 @@ def add_comments(
],
)
self.add_comments(position, comment.replies)
self.add_comments(position, comment._extra_replies)
if collapse_all:
self.comments_tree.collapse_all()

Expand Down Expand Up @@ -107,5 +120,24 @@ def do_downvote(self, renderer, treepath):
def on_selection_change(self, selection):
selection = self.comments_tree.get_selection()
model, iter = selection.get_selected()
comment = self.comments_store.get_value(iter, 6)
self.comment_body.set_text(comment.body)
if iter:
comment = self.comments_store.get_value(iter, 6)
self.comment_body.set_text(comment.body)

def comment(self):
if comment := ReplyDialog(
self.get_toplevel(), "Write a comment", self.submission
).run():
self.add_comments(None, [comment], False)
self.submission._extra_replies.append(comment)

def reply(self):
selection = self.comments_tree.get_selection()
model, iter = selection.get_selected()
parent_comment = self.comments_store.get_value(iter, 6) # type: ignore
comment = ReplyDialog(
self.get_toplevel(), "Write a reply", parent_comment
).run()
if comment is not None:
self.add_comments(iter, [comment], False)
parent_comment._extra_replies.append(comment)
1 change: 1 addition & 0 deletions libs/ui/submitions_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def on_selection_change(self, selection):
self.append_more_submissions()

def _add_submission(self, submission: praw.models.Submission):
submission._extra_replies = []
item = []
if self.show_subreddit_name:
item.append(submission.subreddit.display_name)
Expand Down
2 changes: 2 additions & 0 deletions libs/ui/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .labeled_text_ctrl import *
from .labeled_lisst_ctrl import *
from .simple_button import SimpleButton
from .input_dialog import InputDialog
21 changes: 21 additions & 0 deletions libs/ui/utils/input_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from gi.repository import Gtk
from . import LabeledTextbox


class InputDialog(Gtk.Dialog):
def __init__(self, title: str, question: str):
super().__init__(title)
self.box = self.get_content_area()
self.input = LabeledTextbox(question, "")
self.input.textbox.set_activates_default(True)
self.box.add(self.input)
button = self.add_button("Done", Gtk.ResponseType.OK)
button.set_can_default(True)
button.grab_default()

def run(self):
self.show_all()
response = super().run()
text = self.input.get_text()
self.destroy()
return text if response == Gtk.ResponseType.OK else None
17 changes: 17 additions & 0 deletions libs/ui/utils/simple_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import typing
from gi.repository import Gtk


class SimpleButton(Gtk.Button):
def __init__(
self, label: str, callback: typing.Optional[typing.Callable[[], None]]
):
super().__init__(label)
self.set_use_underline(True)
self.callback = callback
if callback:
self.connect("clicked", self.on_clicked)

def on_clicked(self, *args):
self.stop_emission_by_name("clicked")
self.callback()
2 changes: 2 additions & 0 deletions reddy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from libs.ui import main
from libs import threadpool

window: Gtk.Window = main.Main(
title="Reddy",
Expand All @@ -14,3 +15,4 @@
)
window.show_all()
Gtk.main()
threadpool.pools[0].shutdown(False)

0 comments on commit c6a910f

Please sign in to comment.