OSDN Git Service

Center-align Taskbar icons according to screen width
authorBraden Farmer <farmerbb@gmail.com>
Fri, 20 Nov 2020 05:21:42 +0000 (22:21 -0700)
committerBraden Farmer <farmerbb@gmail.com>
Fri, 20 Nov 2020 05:21:42 +0000 (22:21 -0700)
app/src/main/java/com/farmerbb/taskbar/ui/TaskbarController.java
app/src/main/java/com/farmerbb/taskbar/util/U.java
app/src/main/res/layout/tb_taskbar_left.xml
app/src/main/res/layout/tb_taskbar_right.xml
app/src/main/res/layout/tb_taskbar_top_vertical.xml
app/src/main/res/layout/tb_taskbar_vertical.xml
app/src/test/java/com/farmerbb/taskbar/util/UTest.java

index 8bb0e39..e8be6d2 100644 (file)
@@ -1003,67 +1003,66 @@ public class TaskbarController extends UIController {
         float maxRecentsSize = fullLength ? Float.MAX_VALUE : recentsSize;
         int maxScreenSize;
 
+        float baseStart = U.getBaseTaskbarSizeStart(context);
+        float baseEnd = U.getBaseTaskbarSizeEnd(context, sysTrayIconStates);
+        int baseTotal = Math.round(baseStart + baseEnd);
+
+        int diff = Math.round(Math.max(baseStart, baseEnd) - Math.min(baseStart, baseEnd));
+        boolean startIsBigger = Math.max(baseStart, baseEnd) == baseStart;
+
         if(TaskbarPosition.isVertical(context)) {
             maxScreenSize = Math.max(0, display.height
                     - U.getStatusBarHeight(context)
-                    - U.getBaseTaskbarSize(context, sysTrayIconStates));
+                    - baseTotal);
 
             params.height = (int) Math.min(maxRecentsSize, maxScreenSize)
                     + context.getResources().getDimensionPixelSize(R.dimen.tb_divider_size);
 
             if(fullLength) {
                 try {
-                    Space whitespaceTop = layout.findViewById(R.id.whitespace_top);
-                    Space whitespaceBottom = layout.findViewById(R.id.whitespace_bottom);
+                    Space whitespaceStart = layout.findViewById(R.id.whitespace_start);
+                    Space whitespaceEnd = layout.findViewById(R.id.whitespace_end);
                     int height = maxScreenSize - recentsSize;
 
                     if(pref.getBoolean(PREF_CENTERED_ICONS, false)) {
-                        ViewGroup.LayoutParams topParams = whitespaceTop.getLayoutParams();
-                        topParams.height = height / 2;
-                        whitespaceTop.setLayoutParams(topParams);
-
-                        ViewGroup.LayoutParams bottomParams = whitespaceBottom.getLayoutParams();
-                        bottomParams.height = height / 2;
-                        whitespaceBottom.setLayoutParams(bottomParams);
-                    } else if(TaskbarPosition.isBottom(context)) {
-                        ViewGroup.LayoutParams topParams = whitespaceTop.getLayoutParams();
-                        topParams.height = height;
-                        whitespaceTop.setLayoutParams(topParams);
+                        ViewGroup.LayoutParams startParams = whitespaceStart.getLayoutParams();
+                        startParams.height = (height / 2) + (diff / (startIsBigger ? -2 : 2));
+                        whitespaceStart.setLayoutParams(startParams);
+
+                        ViewGroup.LayoutParams endParams = whitespaceEnd.getLayoutParams();
+                        endParams.height = (height / 2) + (diff / (startIsBigger ? 2 : -2));
+                        whitespaceEnd.setLayoutParams(endParams);
                     } else {
-                        ViewGroup.LayoutParams bottomParams = whitespaceBottom.getLayoutParams();
-                        bottomParams.height = height;
-                        whitespaceBottom.setLayoutParams(bottomParams);
+                        ViewGroup.LayoutParams endParams = whitespaceEnd.getLayoutParams();
+                        endParams.height = height;
+                        whitespaceEnd.setLayoutParams(endParams);
                     }
                 } catch (NullPointerException ignored) {}
             }
         } else {
-            maxScreenSize = Math.max(0, display.width - U.getBaseTaskbarSize(context, sysTrayIconStates));
+            maxScreenSize = Math.max(0, display.width - baseTotal);
 
             params.width = (int) Math.min(maxRecentsSize, maxScreenSize)
                     + context.getResources().getDimensionPixelSize(R.dimen.tb_divider_size);
 
             if(fullLength) {
                 try {
-                    Space whitespaceLeft = layout.findViewById(R.id.whitespace_left);
-                    Space whitespaceRight = layout.findViewById(R.id.whitespace_right);
+                    Space whitespaceStart = layout.findViewById(R.id.whitespace_start);
+                    Space whitespaceEnd = layout.findViewById(R.id.whitespace_end);
                     int width = maxScreenSize - recentsSize;
 
                     if(pref.getBoolean(PREF_CENTERED_ICONS, false)) {
-                        ViewGroup.LayoutParams leftParams = whitespaceLeft.getLayoutParams();
-                        leftParams.width = width / 2;
-                        whitespaceLeft.setLayoutParams(leftParams);
-
-                        ViewGroup.LayoutParams rightParams = whitespaceRight.getLayoutParams();
-                        rightParams.width = width / 2;
-                        whitespaceRight.setLayoutParams(rightParams);
-                    } else if(TaskbarPosition.isRight(context)) {
-                        ViewGroup.LayoutParams leftParams = whitespaceLeft.getLayoutParams();
-                        leftParams.width = width;
-                        whitespaceLeft.setLayoutParams(leftParams);
+                        ViewGroup.LayoutParams startParams = whitespaceStart.getLayoutParams();
+                        startParams.width = (width / 2) + (diff / (startIsBigger ? -2 : 2));
+                        whitespaceStart.setLayoutParams(startParams);
+
+                        ViewGroup.LayoutParams endParams = whitespaceEnd.getLayoutParams();
+                        endParams.width = (width / 2) + (diff / (startIsBigger ? 2 : -2));
+                        whitespaceEnd.setLayoutParams(endParams);
                     } else {
-                        ViewGroup.LayoutParams rightParams = whitespaceRight.getLayoutParams();
-                        rightParams.width = width;
-                        whitespaceRight.setLayoutParams(rightParams);
+                        ViewGroup.LayoutParams endParams = whitespaceEnd.getLayoutParams();
+                        endParams.width = width;
+                        whitespaceEnd.setLayoutParams(endParams);
                     }
                 } catch (NullPointerException ignored) {}
             }
index 5209f37..d470854 100644 (file)
@@ -98,7 +98,6 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -716,7 +715,7 @@ public class U {
         SharedPreferences pref = getSharedPreferences(context);
         DisplayInfo display = getDisplayInfo(context);
         float density = display.currentDensity / 160.0f;
-        float baseTaskbarSize = getBaseTaskbarSizeFloat(context) / density;
+        float baseTaskbarSize = getBaseTaskbarSize(context, null) / density;
         int numOfColumns = 0;
 
         float maxScreenSize = TaskbarPosition.isVertical(context)
@@ -1141,19 +1140,11 @@ public class U {
         }
     }
 
-    public static int getBaseTaskbarSize(Context context, Map<Integer, Boolean> sysTrayIconStates) {
-        return Math.round(getBaseTaskbarSizeFloat(context, sysTrayIconStates));
-    }
-
-    private static float getBaseTaskbarSizeFloat(Context context) {
-        return getBaseTaskbarSizeFloat(context, new HashMap<>());
-    }
-
-    private static float getBaseTaskbarSizeFloat(Context context, Map<Integer, Boolean> sysTrayIconStates) {
+    private static float getBaseTaskbarSize(Context context, Map<Integer, Boolean> sysTrayIconStates) {
         return getBaseTaskbarSizeStart(context) + getBaseTaskbarSizeEnd(context, sysTrayIconStates);
     }
 
-    private static float getBaseTaskbarSizeStart(Context context) {
+    public static float getBaseTaskbarSizeStart(Context context) {
         SharedPreferences pref = getSharedPreferences(context);
         float baseTaskbarSize = context.getResources().getDimension(R.dimen.tb_base_size_start_plus_divider);
 
@@ -1186,7 +1177,7 @@ public class U {
         return baseTaskbarSize;
     }
 
-    private static float getBaseTaskbarSizeEnd(Context context, Map<Integer, Boolean> sysTrayIconStates) {
+    public static float getBaseTaskbarSizeEnd(Context context, Map<Integer, Boolean> sysTrayIconStates) {
         SharedPreferences pref = getSharedPreferences(context);
         float baseTaskbarSize = pref.getBoolean(PREF_ALT_BUTTON_CONFIG, false)
                 ? 0 : context.getResources().getDimension(R.dimen.tb_base_size_collapse_button);
@@ -1194,9 +1185,11 @@ public class U {
         if(isSystemTrayEnabled(context)) {
             float sysTraySize = context.getResources().getDimension(R.dimen.tb_systray_size);
 
-            for(Integer key : sysTrayIconStates.keySet()) {
-                if(!sysTrayIconStates.get(key))
-                    sysTraySize -= context.getResources().getDimension(R.dimen.tb_systray_icon_size);
+            if(sysTrayIconStates != null) {
+                for(Integer key : sysTrayIconStates.keySet()) {
+                    if(!sysTrayIconStates.get(key))
+                        sysTraySize -= context.getResources().getDimension(R.dimen.tb_systray_icon_size);
+                }
             }
 
             baseTaskbarSize += sysTraySize;
index a977f23..357d3c2 100644 (file)
                             android:layout_marginRight="2dp" />
 
                         <Space
-                            android:id="@+id/whitespace_left"
+                            android:id="@+id/whitespace_start"
                             android:layout_height="0dp"
                             android:layout_width="0dp" />
 
                             android:clickable="false" />
 
                         <Space
-                            android:id="@+id/whitespace_right"
+                            android:id="@+id/whitespace_end"
                             android:layout_height="0dp"
                             android:layout_width="0dp" />
 
index 1d6fcda..d3677af 100644 (file)
@@ -52,7 +52,7 @@
                     android:clickable="false">
 
                         <Space
-                            android:id="@+id/whitespace_left"
+                            android:id="@+id/whitespace_end"
                             android:layout_height="0dp"
                             android:layout_width="0dp" />
 
@@ -65,7 +65,7 @@
                             android:clickable="false" />
 
                         <Space
-                            android:id="@+id/whitespace_right"
+                            android:id="@+id/whitespace_start"
                             android:layout_height="0dp"
                             android:layout_width="0dp" />
 
index 741c379..d2429eb 100644 (file)
                 android:layout_marginBottom="2dp" />
 
             <Space
-                android:id="@+id/whitespace_top"
+                android:id="@+id/whitespace_start"
                 android:layout_width="0dp"
                 android:layout_height="0dp" />
 
                 android:clickable="false" />
 
             <Space
-                android:id="@+id/whitespace_bottom"
+                android:id="@+id/whitespace_end"
                 android:layout_width="0dp"
                 android:layout_height="0dp" />
 
index e6c8131..30dc02c 100644 (file)
@@ -63,7 +63,7 @@
             android:clickable="false">
 
             <Space
-                android:id="@+id/whitespace_top"
+                android:id="@+id/whitespace_end"
                 android:layout_width="0dp"
                 android:layout_height="0dp" />
 
@@ -75,7 +75,7 @@
                 android:clickable="false" />
 
             <Space
-                android:id="@+id/whitespace_bottom"
+                android:id="@+id/whitespace_start"
                 android:layout_width="0dp"
                 android:layout_height="0dp" />
 
index 73d887a..72a03b2 100644 (file)
@@ -50,8 +50,6 @@ import org.robolectric.shadows.ShadowSettings;
 import org.robolectric.shadows.ShadowToast;
 import org.robolectric.util.ReflectionHelpers;
 
-import java.util.HashMap;
-
 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -518,7 +516,7 @@ public class UTest {
         float initialSize = context.getResources().getDimension(R.dimen.tb_base_size_start_plus_divider);
         initialSize += context.getResources().getDimension(R.dimen.tb_base_size_collapse_button);
         initialSize += context.getResources().getDimension(R.dimen.tb_dashboard_button_size);
-        assertEquals(Math.round(initialSize), U.getBaseTaskbarSize(context, new HashMap<>()));
+        assertEquals(Math.round(initialSize), U.getBaseTaskbarSize(context, null));
     }
 
     @Test
@@ -529,12 +527,12 @@ public class UTest {
         isSystemTrayEnabledAnswer.answer = false;
         float initialSize = context.getResources().getDimension(R.dimen.tb_base_size_start_plus_divider);
         initialSize += context.getResources().getDimension(R.dimen.tb_base_size_collapse_button);
-        assertEquals(Math.round(initialSize), U.getBaseTaskbarSize(context, new HashMap<>()));
+        assertEquals(Math.round(initialSize), U.getBaseTaskbarSize(context, null));
         SharedPreferences prefs = U.getSharedPreferences(context);
         prefs.edit().putBoolean(PREF_DASHBOARD, true).apply();
         float dashboardButtonSize =
                 context.getResources().getDimension(R.dimen.tb_dashboard_button_size);
-        assertEquals(Math.round(initialSize + dashboardButtonSize), U.getBaseTaskbarSize(context, new HashMap<>()));
+        assertEquals(Math.round(initialSize + dashboardButtonSize), U.getBaseTaskbarSize(context, null));
         prefs.edit().remove(PREF_DASHBOARD).apply();
         float navbarButtonsMargin =
                 context.getResources().getDimension(R.dimen.tb_navbar_buttons_margin);
@@ -543,24 +541,24 @@ public class UTest {
         prefs.edit().putBoolean(PREF_BUTTON_BACK, true).apply();
         assertEquals(
                 Math.round(initialSize + navbarButtonsMargin + iconSize),
-                U.getBaseTaskbarSize(context, new HashMap<>())
+                U.getBaseTaskbarSize(context, null)
         );
         prefs.edit().remove(PREF_BUTTON_BACK).apply();
         prefs.edit().putBoolean(PREF_BUTTON_HOME, true).apply();
         assertEquals(
                 Math.round(initialSize + navbarButtonsMargin + iconSize),
-                U.getBaseTaskbarSize(context, new HashMap<>())
+                U.getBaseTaskbarSize(context, null)
         );
         prefs.edit().remove(PREF_BUTTON_HOME).apply();
         prefs.edit().putBoolean(PREF_BUTTON_RECENTS, true).apply();
         assertEquals(
                 Math.round(initialSize + navbarButtonsMargin + iconSize),
-                U.getBaseTaskbarSize(context, new HashMap<>())
+                U.getBaseTaskbarSize(context, null)
         );
         prefs.edit().remove(PREF_BUTTON_RECENTS).apply();
         isSystemTrayEnabledAnswer.answer = true;
         float systemTraySize = context.getResources().getDimension(R.dimen.tb_systray_size);
-        assertEquals(Math.round(initialSize + systemTraySize), U.getBaseTaskbarSize(context, new HashMap<>()));
+        assertEquals(Math.round(initialSize + systemTraySize), U.getBaseTaskbarSize(context, null));
     }
 
     @Test