Skip to content

Commit

Permalink
feat: dynamically wrap message content
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Mar 22, 2024
1 parent ee8903e commit a4bc296
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]

### Changed

- Dumdum client improvements:
- Dynamically wrap message content for larger window sizes

## [0.2.1] - 2024-03-21

### Changed
Expand Down
31 changes: 31 additions & 0 deletions src/dumdum/client/chat_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import collections
import concurrent.futures
import time
from tkinter import Event, StringVar
from tkinter.ttk import Button, Entry, Frame, Label, Treeview
from typing import ContextManager
Expand Down Expand Up @@ -128,6 +129,11 @@ def _on_tree_select(self, event: Event) -> None:


class MessageList(Frame):
UPDATE_RATE = 250
UPDATE_IDLE_PERIOD = 0.75

_last_configured: float | None

def __init__(self, parent: ChatFrame):
super().__init__(parent)

Expand All @@ -141,6 +147,11 @@ def __init__(self, parent: ChatFrame):
self._scroll_frame = ScrollableFrame(self)
self._scroll_frame.grid(row=0, column=0, sticky="nesw")

self._last_configured = None
self.after(self.UPDATE_RATE, self._update_loop)

self.bind("<Configure>", self._on_configure)

def add_message(self, message: Message) -> None:
widget = MessageView(self._scroll_frame.inner, self, message)
widget.grid(row=len(self.messages), column=0, sticky="ew")
Expand All @@ -162,6 +173,21 @@ def set_channel(self, channel: Channel | None) -> None:
for message in self.parent.message_cache.get_messages(channel):
self.add_message(message)

def _on_configure(self, event: Event) -> None:
self._last_configured = time.perf_counter()

def _update_loop(self) -> None:
self.after(self.UPDATE_RATE, self._update_loop)

if self._last_configured is None:
return
elif time.perf_counter() < self._last_configured + self.UPDATE_IDLE_PERIOD:
return

self._last_configured = None
for message in self.messages:
message.wrap_to_width(self.winfo_width())


class MessageView(Frame):
def __init__(self, frame: Frame, parent: MessageList, message: Message) -> None:
Expand All @@ -175,6 +201,11 @@ def __init__(self, frame: Frame, parent: MessageList, message: Message) -> None:
self.content = Label(self, text=message.content, wraplength=1000)
self.content.grid(row=0, column=1, sticky="ew")

def wrap_to_width(self, width: int) -> None:
minimum = 800
maximum = width - self.nick.winfo_reqwidth() - 20
self.content.configure(wraplength=max(minimum, maximum))


class MessageCache:
def __init__(self) -> None:
Expand Down

0 comments on commit a4bc296

Please sign in to comment.