Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use peer widget for minimap #32

Open
Moosems opened this issue Jul 25, 2023 · 16 comments
Open

Use peer widget for minimap #32

Moosems opened this issue Jul 25, 2023 · 16 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Moosems
Copy link

Moosems commented Jul 25, 2023

Using a peer widget matches the tags and you can just reduce the font size :D.

@tomlin7
Copy link
Owner

tomlin7 commented Jul 25, 2023

@Moosems a really good suggestion! that also speeds up the editor a lot!

also tried it out

import tkinter as tk

class TextPeer(tk.Text):
    "boakly's implementation: https://stackoverflow.com/a/58290100/14507110"
    count = 0
    def __init__(self, master, cnf={}, **kw):
        TextPeer.count += 1
        parent = master.master
        peerName = "peer-{}".format(TextPeer.count)
        if str(parent) == ".":
            peerPath = ".{}".format(peerName)
        else:
            peerPath = "{}.{}".format(parent, peerName)

        master.tk.call(master, 'peer', 'create', peerPath, *self._options(cnf, kw))
        tk.BaseWidget._setup(self, parent, {'name': peerName})

root = tk.Tk()

text_widget1 = tk.Text(root, font="Consolas 13")
text_widget1.tag_config("red", foreground="red")
text_widget1.insert("1.0", "This is the first text widget.", "red")
text_widget1.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

text_widget2 = TextPeer(text_widget1, font="Ariel 5", width=30)
text_widget2.pack(side=tk.LEFT, fill=tk.Y)

root.mainloop()

image
(don't mind the wrap)

it works amazing, and the tags is ofc a plus, current minimap don't have tags added (it was already too much updation going on every change made in editor).

@tomlin7
Copy link
Owner

tomlin7 commented Jul 25, 2023

@Moosems I had an attempt to optimize the minimap further by using lines instead of text (so to render the details less)

self.text = self.tw.get_all_text()
y = 0
offset = 5
for line in self.text.split("\n"):
    if ln := len(line):
        self.cw.create_line(offset, y, ln, y, fill="grey", width=2, tag="redrawn")
    y += 3

image

with splitting for spaces

self.text = self.tw.get_all_text()
y = 0
offset = 5
for line in self.text.split("\n"):
    if line.strip():
        x = offset
        for block in line.split():
            if ln := len(block.strip()):
                self.cw.create_line(x, y, x+ln, y, fill="grey", width=2, tag="redrawn")
                x += ln
            x += 1
    y += 3

image
image

this implementation didn't support indentations at beginning, also no tags. still, this was too much for a single change made in editor too, needed a lot more optimizations.

@tomlin7 tomlin7 added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jul 25, 2023
@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

How fast is the peer widget with timed tests?

@tomlin7
Copy link
Owner

tomlin7 commented Jul 25, 2023

Average execution time: 0.000263 seconds
...

text_widget2 = TextPeer(text_widget1, font="Ariel 5", width=30)
text_widget2.pack(side=tk.LEFT, fill=tk.Y)

import timeit

def measure_widget_performance():
    text_widget1.insert("1.0", "This is the first text widget.", "red")

execution_time = timeit.timeit(measure_widget_performance, number=10)
print(f"Average execution time: {execution_time / 10:.6f} seconds")

...
root.mainloop()

@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

Make 1000 lines ;).

@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

I want to see how it handles a large text file.

@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

For each word you could check any colors associated from tags so at least the user can get the colors if not the actual words. I do like the idea of a peer widget a little better because it has all the tags and words but the scaling ratio could be difficult and though I get the want for optimization I do actually appreciate being able to see the words. At the very least having no wrap with side to side scrolling and indentations would be nice and colored words would similarly be nice.

@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

x = offset + (len(line) - len(line.strip()))

@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

if ln := len(block.strip()): -> if ln := len(block):

@Moosems
Copy link
Author

Moosems commented Jul 25, 2023

Whichever way works better :).

@tomlin7
Copy link
Owner

tomlin7 commented Jul 25, 2023

execution_time = timeit.timeit(measure_widget_performance, number=1000)
print(f"Average execution time: {execution_time / 10:.6f} seconds")
Average execution time: 0.000550 seconds

@tomlin7
Copy link
Owner

tomlin7 commented Jul 25, 2023

x = offset + (len(line) - len(line.strip()))

yess this will work! haha nice solution! ♥

@Moosems
Copy link
Author

Moosems commented Aug 29, 2023

Could you paste the current state of the code?

@tomlin7
Copy link
Owner

tomlin7 commented Aug 30, 2023

I dont have much to show right now, pretty much what i posted above :)) (last week was busy because of uni oop sorry xd)

@tomlin7 tomlin7 added this to the Update cupcake core milestone Aug 30, 2023
@Moosems
Copy link
Author

Moosems commented Aug 30, 2023

Well I don't have the code for the optimized lines.

tomlin7 added a commit that referenced this issue Sep 2, 2023
- uses peer text widget now
- slider removed
@tomlin7
Copy link
Owner

tomlin7 commented Sep 2, 2023

@Moosems check out this recent commit i just did, now uses a peer text widget instead. except, now the slider is not available for minimap. I couldn't render the slider image on top of peer text widget embedded inside the canvas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants