red/code

Broken Showcase/Calculator.red

Closed this issue · 15 comments

The = button does not work. Previous version used to work.

As the last commit removed the spaces around the text of the operators, the typed formula cannot be parsed anymore when attempting to load it.

Possible fix could be to reintroduce the spaces before loading the formula:
formula: copy f/text
replace/all formula "+" " + "
replace/all formula "-" " - "
replace/all formula "/" " / "
replace/all formula "*" " * "
calculation: form math load formula

Thanks for the report!

I've reverted all the changes made to the calculator code. This is supposed to be a "showcase" and the code got ugly, longer and harder to understand than the initial version.

qtxie commented

A side note: On some view backends (GTK for example), the spaces will be rendered, so the UI becomes ugly.

Rats. Do you have a screenshot?

It's something we can note, but this is a pretty big cross platform issue if it affects UI appearance.

qtxie commented

image

qtxie commented

Hmm, Windows also renders the spaces. The main issue is the button size (50x50) is too small for GTK. That's a big issue for cross platform UI. Even in the context of GTK, different theme may has different minimum size of widgets, let alone different OSes.

The screenshot makes the issue clear. Thanks. For a showcase script, we should make sure it looks OK on all platforms, but you're right that we also need to document clearly that we are at the mercy of native controls, so users can't assume it will look the same, or acceptable, and must test their apps.

We can also do some research and make a table of minimum control sizes for each platform, which at least gives people some guidelines.

The interaction with font sizes also makes this a nightmare. But I hear somebody was working on gob!s so we can create a truly portable GUI system. ;^)

9214 commented

@qtxie wouldn't it be more sensible to leave button's text without spaces and inject them into formula on clicking, thus avoiding any rendering issues? Or even better: use trancode on text and construct evaluative block from the get-go.

qtxie commented

we should make sure it looks OK on all platforms

Increase the size of the button a bit (60x60) will make it looks OK on my system.

wouldn't it be more sensible to leave button's text without spaces

Using spaces in button's text is the trick to make the code short.

9214 commented

Using spaces in button's text is the trick to make the code short.

IMO, since this is a showcase, it makes more sense to, well, showcase what Red's lexer is capable of.

Red [
	Title:   "Calculator demo"
	Author:  "Nick Antonaccio"
	File: 	 %calculator.red
	License: "Public domain"
	Needs:	 View
	Notes:   "Very simple calculator app. More from Nick at http://redprogramming.com"
]

view [
	title "Calculator"
	style b: button 50x50 bold font-size 18 [repend f/text [face/text space]]
	b "C" 50x40 [clear f/text]
	f: base 170x40 right white font-size 18 "" return 
	b "1"  b "2"  b "3"  b "+"  return 
	b "4"  b "5"  b "6"  b "-"  return 
	b "7"  b "8"  b "9"  b "*"  return 
	b "0"  b "."  b "/"  b "=" [attempt [f/text: form math transcode f/text]]
]

Thanks @qtxie!

@9214, we need to showcase different aspects of Red. Simplicity is the key for this one.

qtxie commented

style b: button 50x50 bold font-size 18 [repend f/text [space face/text space]]
We need to check if it's an operator before injects spaces.