2

I've been working quite a bit on something I can only describe as a "threaded canvas" for some time now. In a moment I'll describe what I have, but since I'm really open to new ideas, existing solutions or a completely fresh start, I'll formulate the problem.

The canvas is intended to display genetic information (although the specific purpose is somewhat irrelevant). As a conventional text editor, this genetic code is drawn to a canvas, upon which the user can interact with it by typing, selecting, etc. The code is then further decorated with various non-text features such as shapes, lines and colors.

The primary issue here is that significant calculations need to be made prior to displaying certain information.

Consider the following mock-up:

Sample of canvas http://img23.imageshack.us/img23/9931/canvasgv.png

As you can see, the genetic code is monospaced, but the enzyme cuts (shown above the genetic code) are not. Computing where enzymes cut is tedious, as can be displaying features (above, the blue arrow) as there may be very many on-screen. The three-letter codes indicate translations of three blocks of genetic code; although this is performed quickly, typing a single letter into the sequence makes them all shift by one--requiring a recalculation.

Preferably, to speed things up, each of these parts could occur in a separate thread, coming together in the end to compose the final image.

Summarizing: individual parts of the display are computationally difficult, though it is of course desirable that the editor is as responsive as possible.

My present implementation involves performing all draw events in individual threads. By typing, resizing or making selections, a large number of threads may be created, but only the most recent one proceeds to update the display. This guarantees that updating the display takes no longer than one iteration, but doesn't provide snappy feedback of the UI.

I've looked into making modifications to existing editors such as StyledText, but anything more than some boldface and colors makes it significantly slow.

Any suggestions?

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187

2 Answers2

1

Probably your best bet is to embed other SWT widgets in the StyledText. Each widget will perform a certain calculation in a background thread and update its visual representation as the results come in. Note that you can do the calculations in the background but the rendering must happen in the SWT thread. So you can't do many complex things during rendering. If things become too slow, use cache images (create a couple of off-screen images where you can render the results and then simply draw those images).

Extend these widgets from Canvas as this is the widget intended for custom rendering. It will also allow you to react to different events (i.e. display additional information when the user hovers over an enzyme cut).

Be careful with the enzyme cuts, though: They vary in height. I suggest to give this widget a bit more space by default (even when it's not used) so the text doesn't jump a lot while the widget calculates and adds cuts.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Good insights, Aaron. I'll take a look into embedding widgets into StyledText. As for the text jumping around due to the enzymes: I don't deem this very likely since that is fairly slow (lots of enzymes with regular expressions as recognition sequences in a huge file). I think it would actually be a good idea to give them a minimum display delay so that in smaller files it only updates after a second or two. That's really in the details, though; if I can get variable sized widgets into StyledText, I think I'm halfway there. – Paul Lammertsma Sep 29 '09 at 15:34
  • Changing the size of a widget should make StyledText re-layout itself. – Aaron Digulla Sep 29 '09 at 15:52
1

I instead chose for a solution using multiple buffers, where each buffer is drawn in a separate thread. The performance is much better than StyledText, and I have complete flexibility to drawing things where I like.

The only drawback is that I have to reimplement the basics of text editing: text selection, navigation -- even to the basics of entering characters. I am nevertheless satisfied with the result.

I'm afraid I cannot provide source code as it is part of a copyrighted project.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • By "buffers" you mean off-screen GCs? To expand, I've had bad responsiveness on 4K displays when displaying 4K+ images on a Canvas (with zoom, rotation, drag). – LppEdd Oct 11 '21 at 22:36