From 3713e9ed0062c890caec6bb9ec66a4262683ad73 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Wed, 12 Jul 2023 01:42:58 -0400 Subject: [PATCH] Switch color palette to mIRC's palette when saving/opening as mIRC code format --- cspell.json | 1 + src/textual_paint/paint.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/cspell.json b/cspell.json index 97a5152..97468b2 100644 --- a/cspell.json +++ b/cspell.json @@ -37,6 +37,7 @@ "Deutsch", "DIALOGEX", "disambiguates", + "dockable", "domtree", "emacsclient", "executablepath", diff --git a/src/textual_paint/paint.py b/src/textual_paint/paint.py index 6f83ef9..732e069 100755 --- a/src/textual_paint/paint.py +++ b/src/textual_paint/paint.py @@ -2798,6 +2798,7 @@ class PaintApp(App[None]): self.canvas.refresh(layout=True) # awkward to do this in here as well as externally, but this should be updated with the new undo count self.saved_undo_count = len(self.undos) + self.update_palette_from_format_id(AnsiArtDocument.format_from_extension(file_path)) return True except UnicodeDecodeError: self.message_box(_("Open"), file_path + "\n" + _("Paint cannot read this file.") + "\n" + _("Unexpected file format."), "ok") @@ -2809,6 +2810,23 @@ class PaintApp(App[None]): self.message_box(_("Open"), _("An unexpected error occurred while reading %1.", file_path), "ok", error=e) return False + def update_palette_from_format_id(self, format_id: str | None) -> None: + """Update the palette based on the file format. + + In the future, this should update from attributes set when loading the file, + such as whether it supports color, and if not, it could show pattern fills, + such as ░▒▓█... that's not a lot of patterns, and you could get those from the + character picker, but it might be nice to have them more accessible, + that or to make the character picker a dockable window. + """ + global palette + if format_id == "IRC": + palette = irc_palette + [irc_palette[0]] * (len(palette) - len(irc_palette)) + self.query_one(ColorsBox).update_palette() + elif format_id == "PLAINTEXT": + palette = ["#000000", "#ffffff"] + ["#ffffff"] * (len(palette) - 2) + self.query_one(ColorsBox).update_palette() + async def save(self) -> bool: """Save the image to a file. @@ -3226,6 +3244,9 @@ class PaintApp(App[None]): self.canvas.image = self.image = new_image self.canvas.refresh(layout=True) self.file_path = file_path + self.update_palette_from_format_id(AnsiArtDocument.format_from_extension(file_path)) + # Should this set self.saved_undo_count? + # I guess it's probably always 0 at this point, right? opened_callback() self.recover_from_backup() if self.is_document_modified():