Browse Source

Tables: Store submitted column width and avoid saving default default widths.

omar 5 years ago
parent
commit
c96c84b6dc
2 changed files with 19 additions and 14 deletions
  1. 1 0
      imgui_internal.h
  2. 18 14
      imgui_tables.cpp

+ 1 - 0
imgui_internal.h

@@ -1897,6 +1897,7 @@ struct ImGuiTableColumn
     ImGuiTableColumnFlags   Flags;                          // Effective flags. See ImGuiTableColumnFlags_
     ImGuiTableColumnFlags   Flags;                          // Effective flags. See ImGuiTableColumnFlags_
     float                   MinX;                           // Absolute positions
     float                   MinX;                           // Absolute positions
     float                   MaxX;
     float                   MaxX;
+    float                   WidthOrWeightInitValue;         // Value passed to TableSetupColumn()
     float                   WidthStretchWeight;             // Master width weight when (Flags & _WidthStretch). Often around ~1.0f initially.
     float                   WidthStretchWeight;             // Master width weight when (Flags & _WidthStretch). Often around ~1.0f initially.
     float                   WidthRequest;                   // Master width absolute value when !(Flags & _WidthStretch). When Stretch this is derived every frame from WidthStretchWeight in TableUpdateLayout()
     float                   WidthRequest;                   // Master width absolute value when !(Flags & _WidthStretch). When Stretch this is derived every frame from WidthStretchWeight in TableUpdateLayout()
     float                   WidthGiven;                     // Final/actual width visible == (MaxX - MinX), locked in TableUpdateLayout(). May be >WidthRequest to honor minimum width, may be <WidthRequest to honor shrinking columns down in tight space.
     float                   WidthGiven;                     // Final/actual width visible == (MaxX - MinX), locked in TableUpdateLayout(). May be >WidthRequest to honor minimum width, may be <WidthRequest to honor shrinking columns down in tight space.

+ 18 - 14
imgui_tables.cpp

@@ -1529,25 +1529,26 @@ void    ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags,
     flags = column->Flags;
     flags = column->Flags;
 
 
     // Initialize defaults
     // Initialize defaults
-    // FIXME-TABLE: We don't restore widths/weight so let's avoid using IsSettingsLoaded for now
-    if (table->IsInitializing && column->WidthRequest < 0.0f && column->WidthStretchWeight < 0.0f)// && !table->IsSettingsLoaded)
+    if (flags & ImGuiTableColumnFlags_WidthStretch)
+    {
+        IM_ASSERT(init_width_or_weight != 0.0f && "Need to provide a valid weight!");
+        if (init_width_or_weight < 0.0f)
+            init_width_or_weight = 1.0f;
+    }
+    column->WidthOrWeightInitValue = init_width_or_weight;
+    if (table->IsInitializing && column->WidthRequest < 0.0f && column->WidthStretchWeight < 0.0f)
     {
     {
         // Init width or weight
         // Init width or weight
-        // Disable auto-fit if a default fixed width has been specified
         if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f)
         if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f)
         {
         {
+            // Disable auto-fit if a default fixed width has been specified
             column->WidthRequest = init_width_or_weight;
             column->WidthRequest = init_width_or_weight;
             column->AutoFitQueue = 0x00;
             column->AutoFitQueue = 0x00;
         }
         }
         if (flags & ImGuiTableColumnFlags_WidthStretch)
         if (flags & ImGuiTableColumnFlags_WidthStretch)
-        {
-            IM_ASSERT(init_width_or_weight < 0.0f || init_width_or_weight > 0.0f);
-            column->WidthStretchWeight = (init_width_or_weight < 0.0f ? 1.0f : init_width_or_weight);
-        }
+            column->WidthStretchWeight = init_width_or_weight;
         else
         else
-        {
             column->WidthStretchWeight = 1.0f;
             column->WidthStretchWeight = 1.0f;
-        }
     }
     }
     if (table->IsInitializing)
     if (table->IsInitializing)
     {
     {
@@ -2087,7 +2088,7 @@ void    ImGui::TableAutoHeaders()
     // FIXME-TABLE: TableOpenContextMenu() is not public yet.
     // FIXME-TABLE: TableOpenContextMenu() is not public yet.
     if (IsMouseReleased(1) && TableGetHoveredColumn() == columns_count)
     if (IsMouseReleased(1) && TableGetHoveredColumn() == columns_count)
         if (g.IO.MousePos.y >= row_y1 && g.IO.MousePos.y < row_y1 + row_height)
         if (g.IO.MousePos.y >= row_y1 && g.IO.MousePos.y < row_y1 + row_height)
-            TableOpenContextMenu(table, -1); // Will open a non-column-specific popup.    
+            TableOpenContextMenu(table, -1); // Will open a non-column-specific popup.
 }
 }
 
 
 // Emit a column header (text + optional sort order)
 // Emit a column header (text + optional sort order)
@@ -2475,12 +2476,12 @@ void ImGui::TableSaveSettings(ImGuiTable* table)
     ImGuiTableColumn* column = table->Columns.Data;
     ImGuiTableColumn* column = table->Columns.Data;
     ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
     ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings();
 
 
-    // FIXME-TABLE: Logic to avoid saving default widths?
     bool save_ref_scale = false;
     bool save_ref_scale = false;
-    settings->SaveFlags = ImGuiTableFlags_Resizable;
+    settings->SaveFlags = ImGuiTableFlags_None;
     for (int n = 0; n < table->ColumnsCount; n++, column++, column_settings++)
     for (int n = 0; n < table->ColumnsCount; n++, column++, column_settings++)
     {
     {
-        column_settings->WidthOrWeight = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? column->WidthStretchWeight : column->WidthRequest;
+        const float width_or_weight = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? column->WidthStretchWeight : column->WidthRequest;
+        column_settings->WidthOrWeight = width_or_weight;
         column_settings->Index = (ImS8)n;
         column_settings->Index = (ImS8)n;
         column_settings->DisplayOrder = column->DisplayOrder;
         column_settings->DisplayOrder = column->DisplayOrder;
         column_settings->SortOrder = column->SortOrder;
         column_settings->SortOrder = column->SortOrder;
@@ -2490,8 +2491,11 @@ void ImGui::TableSaveSettings(ImGuiTable* table)
         if ((column->Flags & ImGuiTableColumnFlags_WidthStretch) == 0)
         if ((column->Flags & ImGuiTableColumnFlags_WidthStretch) == 0)
             save_ref_scale = true;
             save_ref_scale = true;
 
 
-        // We skip saving some data in the .ini file when they are unnecessary to restore our state
+        // We skip saving some data in the .ini file when they are unnecessary to restore our state.
+        // Note that fixed width where initial width was derived from auto-fit will always be saved as WidthOrWeightInitValue will be 0.0f.
         // FIXME-TABLE: We don't have logic to easily compare SortOrder to DefaultSortOrder yet so it's always saved when present.
         // FIXME-TABLE: We don't have logic to easily compare SortOrder to DefaultSortOrder yet so it's always saved when present.
+        if (width_or_weight != column->WidthOrWeightInitValue)
+            settings->SaveFlags |= ImGuiTableFlags_Resizable;
         if (column->DisplayOrder != n)
         if (column->DisplayOrder != n)
             settings->SaveFlags |= ImGuiTableFlags_Reorderable;
             settings->SaveFlags |= ImGuiTableFlags_Reorderable;
         if (column->SortOrder != -1)
         if (column->SortOrder != -1)