forked from mbk-lab/rui_orig
2
0
Fork 0

Added the UserAgent function to the Session interface

This commit is contained in:
Alexei Anoshenko 2022-01-29 11:20:05 -05:00
parent 1a7a7e9daf
commit f989952817
4 changed files with 64 additions and 13 deletions

View File

@ -42,9 +42,19 @@ function socketOpen() {
} }
} }
const lang = window.navigator.languages; const lang = window.navigator.language;
if (lang) { if (lang) {
message += ",languages=\"" + lang + "\""; message += ",language=\"" + lang + "\"";
}
const langs = window.navigator.languages;
if (langs) {
message += ",languages=\"" + langs + "\"";
}
const userAgent = window.navigator.userAgent
if (userAgent) {
message += ",user-agent=\"" + userAgent + "\"";
} }
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)"); const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");

View File

@ -74,7 +74,13 @@ func textCanvasDemo(canvas rui.Canvas) {
canvas.SetTextAlign(rui.LeftAlign) canvas.SetTextAlign(rui.LeftAlign)
canvas.SetTextBaseline(rui.TopBaseline) canvas.SetTextBaseline(rui.TopBaseline)
if canvas.View().Session().DarkTheme() {
canvas.SetSolidColorFillStyle(0xFFFFFFFF)
canvas.SetSolidColorStrokeStyle(0xFFFFFFFF)
} else {
canvas.SetSolidColorFillStyle(0xFF000000) canvas.SetSolidColorFillStyle(0xFF000000)
canvas.SetSolidColorStrokeStyle(0xFF000000)
}
canvas.FillText(10, 10, "Default font") canvas.FillText(10, 10, "Default font")
canvas.StrokeText(300, 10, "Default font") canvas.StrokeText(300, 10, "Default font")
@ -136,7 +142,11 @@ func textCanvasDemo(canvas rui.Canvas) {
func textAlignCanvasDemo(canvas rui.Canvas) { func textAlignCanvasDemo(canvas rui.Canvas) {
canvas.Save() canvas.Save()
canvas.SetFont("sans-serif", rui.Pt(10)) canvas.SetFont("sans-serif", rui.Pt(10))
if canvas.View().Session().DarkTheme() {
canvas.SetSolidColorFillStyle(0xFFFFFFFF)
} else {
canvas.SetSolidColorFillStyle(0xFF000000) canvas.SetSolidColorFillStyle(0xFF000000)
}
canvas.SetSolidColorStrokeStyle(0xFF00FFFF) canvas.SetSolidColorStrokeStyle(0xFF00FFFF)
baseline := []string{"Alphabetic", "Top", "Middle", "Bottom", "Hanging", "Ideographic"} baseline := []string{"Alphabetic", "Top", "Middle", "Bottom", "Hanging", "Ideographic"}
@ -176,18 +186,28 @@ func lineStyleCanvasDemo(canvas rui.Canvas) {
canvas.SetLineWidth(1) canvas.SetLineWidth(1)
y := float64(40 + 20*i) y := float64(40 + 20*i)
canvas.DrawLine(10, y, 180, y) canvas.DrawLine(10, y, 180, y)
if canvas.View().Session().DarkTheme() {
canvas.SetSolidColorStrokeStyle(0xFFFFFFFF)
} else {
canvas.SetSolidColorStrokeStyle(0xFF000000) canvas.SetSolidColorStrokeStyle(0xFF000000)
}
canvas.SetLineWidth(10) canvas.SetLineWidth(10)
canvas.SetLineCap(i) canvas.SetLineCap(i)
canvas.DrawLine(20, y, 170, y) canvas.DrawLine(20, y, 170, y)
canvas.FillText(200, y, cap) canvas.FillText(200, y, cap)
} }
canvas.SetSolidColorStrokeStyle(0xFF0000FF) if canvas.View().Session().DarkTheme() {
canvas.SetSolidColorStrokeStyle(0xFFFFFFFF)
canvas.SetSolidColorFillStyle(0xFF00FFFF)
} else {
canvas.SetSolidColorStrokeStyle(0xFF000000)
canvas.SetSolidColorFillStyle(0xFF0000FF)
}
canvas.SetFont("courier", rui.Pt(12)) canvas.SetFont("courier", rui.Pt(12))
canvas.FillText(80, 115, "SetLineJoin(...)") canvas.FillText(80, 115, "SetLineJoin(...)")
canvas.SetSolidColorStrokeStyle(0xFF000000)
canvas.SetLineWidth(10) canvas.SetLineWidth(10)
canvas.SetLineCap(rui.ButtCap) canvas.SetLineCap(rui.ButtCap)
@ -206,14 +226,11 @@ func lineStyleCanvasDemo(canvas rui.Canvas) {
canvas.StrokePath(path) canvas.StrokePath(path)
canvas.FillText(210, y+20, join) canvas.FillText(210, y+20, join)
} }
canvas.SetSolidColorStrokeStyle(0xFF0000FF)
canvas.SetFont("courier", rui.Pt(12)) canvas.SetFont("courier", rui.Pt(12))
canvas.FillText(20, 300, "SetLineDash([]float64{16, 8, 4, 8}, ...)") canvas.FillText(20, 300, "SetLineDash([]float64{16, 8, 4, 8}, ...)")
canvas.SetFont("courier", rui.Pt(10)) canvas.SetFont("courier", rui.Pt(10))
canvas.SetLineDash([]float64{16, 8, 4, 8}, 0) canvas.SetLineDash([]float64{16, 8, 4, 8}, 0)
canvas.SetSolidColorStrokeStyle(0xFF000000)
canvas.SetLineWidth(4) canvas.SetLineWidth(4)
canvas.SetLineCap(rui.ButtCap) canvas.SetLineCap(rui.ButtCap)
@ -246,8 +263,13 @@ func transformCanvasDemo(canvas rui.Canvas) {
y1 := y0 + float64((ny-1)*20) y1 := y0 + float64((ny-1)*20)
canvas.SetFont("serif", rui.Pt(10)) canvas.SetFont("serif", rui.Pt(10))
canvas.SetSolidColorFillStyle(rui.Black) if canvas.View().Session().DarkTheme() {
canvas.SetSolidColorStrokeStyle(0xFFFFFFFF)
canvas.SetSolidColorFillStyle(0xFFFFFFFF)
} else {
canvas.SetSolidColorStrokeStyle(0xFF000000)
canvas.SetSolidColorFillStyle(0xFF000000)
}
canvas.SetTextAlign(rui.CenterAlign) canvas.SetTextAlign(rui.CenterAlign)
canvas.SetTextBaseline(rui.BottomBaseline) canvas.SetTextBaseline(rui.BottomBaseline)
for i := 0; i < nx; i++ { for i := 0; i < nx; i++ {
@ -266,7 +288,11 @@ func transformCanvasDemo(canvas rui.Canvas) {
} }
canvas.SetFont("courier", rui.Pt(14)) canvas.SetFont("courier", rui.Pt(14))
canvas.SetSolidColorFillStyle(rui.Black) if canvas.View().Session().DarkTheme() {
canvas.SetSolidColorFillStyle(0xFFFFFFFF)
} else {
canvas.SetSolidColorFillStyle(0xFF000000)
}
canvas.SetTextAlign(rui.CenterAlign) canvas.SetTextAlign(rui.CenterAlign)
canvas.SetTextBaseline(rui.TopBaseline) canvas.SetTextBaseline(rui.TopBaseline)

View File

@ -33,6 +33,8 @@ type Session interface {
Color(tag string) (Color, bool) Color(tag string) (Color, bool)
// SetCustomTheme set the custom theme // SetCustomTheme set the custom theme
SetCustomTheme(name string) bool SetCustomTheme(name string) bool
// UserAgent() returns the "user-agent" text of the client browser
UserAgent() string
// Language returns the current session language // Language returns the current session language
Language() string Language() string
// SetLanguage set the current session language // SetLanguage set the current session language
@ -106,6 +108,7 @@ type sessionData struct {
touchScreen bool touchScreen bool
textDirection int textDirection int
pixelRatio float64 pixelRatio float64
userAgent string
language string language string
languages []string languages []string
checkboxOff string checkboxOff string
@ -148,12 +151,20 @@ func newSession(app Application, id int, customTheme string, params DataObject)
session.touchScreen = (value == "1" || value == "true") session.touchScreen = (value == "1" || value == "true")
} }
if value, ok := params.PropertyValue("user-agent"); ok {
session.userAgent = value
}
if value, ok := params.PropertyValue("direction"); ok { if value, ok := params.PropertyValue("direction"); ok {
if value == "rtl" { if value == "rtl" {
session.textDirection = RightToLeftDirection session.textDirection = RightToLeftDirection
} }
} }
if value, ok := params.PropertyValue("language"); ok {
session.language = value
}
if value, ok := params.PropertyValue("languages"); ok { if value, ok := params.PropertyValue("languages"); ok {
session.languages = strings.Split(value, ",") session.languages = strings.Split(value, ",")
} }

View File

@ -328,6 +328,10 @@ func (session *sessionData) radiobuttonOnImage() string {
return session.radiobuttonOn return session.radiobuttonOn
} }
func (session *sessionData) UserAgent() string {
return session.userAgent
}
func (session *sessionData) Language() string { func (session *sessionData) Language() string {
if session.language != "" { if session.language != "" {
return session.language return session.language