Setting a label's MinSize will cause label right-alignment to fail.
ddkwork opened this issue · 3 comments
This may not be a bug, but there is no discussion forum, so I can only ask you for advice here, and I will close the subtopic if I am bothering you
func NewStructView[T any](data T, w *unison.Window) *StructView[T] {
// ux.displayEditor()//todo implement me
fields := reflect.VisibleFields(reflect.TypeOf(data))
editors := make([]StructEditor, len(fields))
for i, field := range fields {
value := reflect.ValueOf(data).Field(i)
switch v := value.Interface().(type) { // todo unMarshal type by field type
case string:
editors[i] = StructEditor{StructField: field, value: v}
case int:
// reflect.ValueOf(data).Field(i).SetInt(int64(v))
editors[i] = StructEditor{field, strconv.Itoa(v)}
case int8:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case int16:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case int32:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case int64:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case uint:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case uint8:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case uint16:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case uint32:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case uint64:
editors[i] = StructEditor{field, strconv.Itoa(int(v))}
case float32:
editors[i] = StructEditor{field, strconv.FormatFloat(float64(v), 'f', -1, 32)}
case float64:
editors[i] = StructEditor{field, strconv.FormatFloat(v, 'f', -1, 64)}
case bool:
editors[i] = StructEditor{field, strconv.FormatBool(v)}
case []byte:
editors[i] = StructEditor{field, string(v)}
case []string, []int, []int8, []int16, []int32, []int64, []uint, []uint16, []uint32, []uint64, []float32, []float64, []bool:
marshalIndent, err := json.MarshalIndent(v, "", " ")
mylog.Check(err)
editors[i] = StructEditor{field, string(marshalIndent)}
case map[string]string, map[string]int, map[string]int8, map[string]int16, map[string]int32, map[string]int64, map[string]uint, map[string]uint8, map[string]uint16, map[string]uint32, map[string]uint64, map[string]float32, map[string]float64, map[string]bool:
marshalIndent, err := json.MarshalIndent(v, "", " ")
mylog.Check(err)
editors[i] = StructEditor{field, string(marshalIndent)}
default:
// panic("unsupported type")//todo handle error
}
}
v := &StructView[T]{
Panel: unison.Panel{},
target: data,
previousFocusKey: "",
svg: nil,
undoMgr: nil,
scroll: nil,
applyButton: nil,
cancelButton: nil,
// beforeData: nil,
// editorData: nil,
modificationCallback: nil,
preApplyCallback: nil,
scale: 0,
promptForSave: false,
}
v.Self = v
PanelSetFlexLayout(v.AsPanel(), 1)
addVSpacer(w.Content(), nil)
var maxLabelSize unison.Size
for _, editor := range editors {
text := unison.NewText(editor.Name, &unison.TextDecoration{
Font: unison.LabelFont,
Foreground: nil,
Background: nil,
BaselineOffset: 0,
Underline: false,
StrikeThrough: false,
})
labelSize := unison.LabelSize(text, nil, side.Right, 0)
maxLabelSize = labelSize.Max(maxLabelSize)
}
for i, editor := range editors {
row := unison.NewPanel()
row.SetBorder(unison.NewEmptyBorder(unison.NewUniformInsets(unison.StdHSpacing / 2)))
row.SetLayout(&unison.FlexLayout{
Columns: 2,
HSpacing: unison.StdHSpacing,
VSpacing: unison.StdVSpacing, // mock table row style
HAlign: 0,
VAlign: 0,
})
// addNameLabelAndField(row, editor.Name, &editor.value)
// label := NewFieldLeadingLabel(i18n.Text(editor.Name), false)
label := unison.NewLabel()
label.Text = i18n.Text(editor.Name)
label.SetLayoutData(&unison.FlexLayoutData{
SizeHint: unison.Size{},
MinSize: maxLabelSize,
HSpan: 0,
VSpan: 0,
HAlign: align.End, // 右对齐,todo bug
VAlign: align.Middle, // 垂直居中
HGrab: true,
VGrab: true,
})
row.AddChild(label)
value := NewField2()
value.SetLayoutData(&unison.FlexLayoutData{
HAlign: align.Fill,
HGrab: true,
})
NewFieldContextMenuItems(value)
if i%2 == 1 {
value.EditableInk = unison.RGB(54, 57, 61)
}
value.SetText(editor.value)
value.SetLayoutData(&unison.FlexLayoutData{
SizeHint: unison.Size{},
MinSize: unison.Size{},
HSpan: 0,
VSpan: 0,
HAlign: align.Start,
HGrab: true,
VGrab: true,
})
row.AddChild(value)
v.AddChild(row)
}
NewButtonsPanel(v.AsPanel(),
[]string{"save", "cancel"},
func() {
},
func() {
},
)
//NewApplyCancelButtonPanel(v.AsPanel(),
// func() {
// },
// func() {
// },
//)
NewScrollPanelHintedFill(v.AsPanel(), w.Content())
return v
}
It looks like you're setting the minimum size of the label, which means its the label's alignment that will place it within that minimum space, not the layout, since the layout is using the minimum space for them all.
It looks like you're setting the minimum size of the label, which means its the label's alignment that will place it within that minimum space, not the layout, since the layout is using the minimum space for them all.
I seem to understand what you mean, I should change the layout mode to trigger the tab columns to be right-aligned instead of forcing the size to be specified. Previously, in order to achieve tablist alignment, the max tab size was obtained by traversing, and a second traversal assigned the max tab to the min size of each tab. This process, as you mentioned, did not use the layout to trigger its alignment operation, listen to your words, it is better than reading ten years of books ah, thank you very much for solving the problem. I would like to try to arrange the tab list in a layout to trigger the right alignment.