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

Add WidgetBase.SceneSize #1259

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions core/sizeclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package core

import "cogentcore.org/core/math32"

// SizeClasses are the different size classes that a window can have.
type SizeClasses int32 //enums:enum -trim-prefix Size

Expand All @@ -21,17 +23,25 @@ const (
SizeExpanded
)

// SizeClass returns the size class of the scene in which the widget is contained.
func (wb *WidgetBase) SizeClass() SizeClasses {
dots := float32(wb.Scene.SceneGeom.Size.X)
// SceneSize returns the effective size of the scene in which the widget is contained
// in terms of dp (density-independent pixels).
func (wb *WidgetBase) SceneSize() math32.Vector2 {
dots := math32.FromPoint(wb.Scene.SceneGeom.Size)
if wb.Scene.hasFlag(scenePrefSizing) {
if currentRenderWindow != nil {
rg := currentRenderWindow.SystemWindow.RenderGeom()
dots = float32(rg.Size.X)
dots = math32.FromPoint(rg.Size)
}
}
dpd := wb.Scene.Styles.UnitContext.Dp(1) // dots per dp
dp := dots / dpd // dots / (dots / dp) = dots * (dp / dots) = dp
dp := dots.DivScalar(dpd) // dots / (dots / dp) = dots * (dp / dots) = dp
return dp
}

// SizeClass returns the size class of the scene in which the widget is contained
// based on [WidgetBase.SceneSize].
func (wb *WidgetBase) SizeClass() SizeClasses {
dp := wb.SceneSize().X
switch {
case dp < 600:
return SizeCompact
Expand Down
Loading