From f989952817b3d348dec7fee752cdd9f5f8fb7984 Mon Sep 17 00:00:00 2001 From: Alexei Anoshenko Date: Sat, 29 Jan 2022 11:20:05 -0500 Subject: [PATCH] Added the UserAgent function to the Session interface --- app_scripts.js | 14 ++++++++++++-- demo/canvasDemo.go | 48 +++++++++++++++++++++++++++++++++++----------- session.go | 11 +++++++++++ sessionTheme.go | 4 ++++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/app_scripts.js b/app_scripts.js index 526f43b..427fa36 100644 --- a/app_scripts.js +++ b/app_scripts.js @@ -42,9 +42,19 @@ function socketOpen() { } } - const lang = window.navigator.languages; + const lang = window.navigator.language; 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)"); diff --git a/demo/canvasDemo.go b/demo/canvasDemo.go index e9ad43e..fca814e 100644 --- a/demo/canvasDemo.go +++ b/demo/canvasDemo.go @@ -74,7 +74,13 @@ func textCanvasDemo(canvas rui.Canvas) { canvas.SetTextAlign(rui.LeftAlign) canvas.SetTextBaseline(rui.TopBaseline) - canvas.SetSolidColorFillStyle(0xFF000000) + if canvas.View().Session().DarkTheme() { + canvas.SetSolidColorFillStyle(0xFFFFFFFF) + canvas.SetSolidColorStrokeStyle(0xFFFFFFFF) + } else { + canvas.SetSolidColorFillStyle(0xFF000000) + canvas.SetSolidColorStrokeStyle(0xFF000000) + } canvas.FillText(10, 10, "Default font") canvas.StrokeText(300, 10, "Default font") @@ -136,7 +142,11 @@ func textCanvasDemo(canvas rui.Canvas) { func textAlignCanvasDemo(canvas rui.Canvas) { canvas.Save() canvas.SetFont("sans-serif", rui.Pt(10)) - canvas.SetSolidColorFillStyle(0xFF000000) + if canvas.View().Session().DarkTheme() { + canvas.SetSolidColorFillStyle(0xFFFFFFFF) + } else { + canvas.SetSolidColorFillStyle(0xFF000000) + } canvas.SetSolidColorStrokeStyle(0xFF00FFFF) baseline := []string{"Alphabetic", "Top", "Middle", "Bottom", "Hanging", "Ideographic"} @@ -176,18 +186,28 @@ func lineStyleCanvasDemo(canvas rui.Canvas) { canvas.SetLineWidth(1) y := float64(40 + 20*i) canvas.DrawLine(10, y, 180, y) - canvas.SetSolidColorStrokeStyle(0xFF000000) + if canvas.View().Session().DarkTheme() { + canvas.SetSolidColorStrokeStyle(0xFFFFFFFF) + } else { + canvas.SetSolidColorStrokeStyle(0xFF000000) + } + canvas.SetLineWidth(10) canvas.SetLineCap(i) canvas.DrawLine(20, y, 170, y) 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.FillText(80, 115, "SetLineJoin(...)") - canvas.SetSolidColorStrokeStyle(0xFF000000) canvas.SetLineWidth(10) canvas.SetLineCap(rui.ButtCap) @@ -206,14 +226,11 @@ func lineStyleCanvasDemo(canvas rui.Canvas) { canvas.StrokePath(path) canvas.FillText(210, y+20, join) } - - canvas.SetSolidColorStrokeStyle(0xFF0000FF) canvas.SetFont("courier", rui.Pt(12)) canvas.FillText(20, 300, "SetLineDash([]float64{16, 8, 4, 8}, ...)") canvas.SetFont("courier", rui.Pt(10)) canvas.SetLineDash([]float64{16, 8, 4, 8}, 0) - canvas.SetSolidColorStrokeStyle(0xFF000000) canvas.SetLineWidth(4) canvas.SetLineCap(rui.ButtCap) @@ -246,8 +263,13 @@ func transformCanvasDemo(canvas rui.Canvas) { y1 := y0 + float64((ny-1)*20) 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.SetTextBaseline(rui.BottomBaseline) for i := 0; i < nx; i++ { @@ -266,7 +288,11 @@ func transformCanvasDemo(canvas rui.Canvas) { } 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.SetTextBaseline(rui.TopBaseline) diff --git a/session.go b/session.go index 505a78d..75dcde4 100644 --- a/session.go +++ b/session.go @@ -33,6 +33,8 @@ type Session interface { Color(tag string) (Color, bool) // SetCustomTheme set the custom theme SetCustomTheme(name string) bool + // UserAgent() returns the "user-agent" text of the client browser + UserAgent() string // Language returns the current session language Language() string // SetLanguage set the current session language @@ -106,6 +108,7 @@ type sessionData struct { touchScreen bool textDirection int pixelRatio float64 + userAgent string language string languages []string checkboxOff string @@ -148,12 +151,20 @@ func newSession(app Application, id int, customTheme string, params DataObject) 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 == "rtl" { session.textDirection = RightToLeftDirection } } + if value, ok := params.PropertyValue("language"); ok { + session.language = value + } + if value, ok := params.PropertyValue("languages"); ok { session.languages = strings.Split(value, ",") } diff --git a/sessionTheme.go b/sessionTheme.go index f6642cf..3e9d149 100644 --- a/sessionTheme.go +++ b/sessionTheme.go @@ -328,6 +328,10 @@ func (session *sessionData) radiobuttonOnImage() string { return session.radiobuttonOn } +func (session *sessionData) UserAgent() string { + return session.userAgent +} + func (session *sessionData) Language() string { if session.language != "" { return session.language