-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AdaptiveActionsToolbar for better context bar controls.
- Loading branch information
1 parent
ecf7a41
commit 7c729c2
Showing
5 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/src/main/java/org/thoughtcrime/securesms/util/views/AdaptiveActionsToolbar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.thoughtcrime.securesms.util.views; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.util.AttributeSet; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.widget.Toolbar; | ||
|
||
import org.thoughtcrime.securesms.R; | ||
import org.thoughtcrime.securesms.util.ViewUtil; | ||
|
||
/** | ||
* AdaptiveActionsToolbar behaves like a normal {@link Toolbar} except in that it ignores the | ||
* showAsAlways attributes of menu items added via menu inflation, opting for an adaptive algorithm | ||
* instead. This algorithm will display as many icons as it can up to a specific percentage of the | ||
* screen. | ||
* | ||
* Each ActionView icon is expected to occupy 48dp of space, including padding. Items are stacked one | ||
* after the next with no margins. | ||
* | ||
* This view can be customized via attributes: | ||
* | ||
* aat_max_shown -- controls the max number of items to display. | ||
* aat_percent_for_actions -- controls the max percent of screen width the buttons can occupy. | ||
*/ | ||
public class AdaptiveActionsToolbar extends Toolbar { | ||
|
||
private static final int NAVIGATION_SP = 48; | ||
private static final int ACTION_VIEW_WIDTH_SP = 42; | ||
private static final int OVERFLOW_VIEW_WIDTH_SP = 35; | ||
|
||
private int maxShown; | ||
|
||
public AdaptiveActionsToolbar(@NonNull Context context) { | ||
this(context, null); | ||
} | ||
|
||
public AdaptiveActionsToolbar(@NonNull Context context, @Nullable AttributeSet attrs) { | ||
this(context, attrs, R.attr.toolbarStyle); | ||
} | ||
|
||
public AdaptiveActionsToolbar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
|
||
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AdaptiveActionsToolbar); | ||
|
||
maxShown = array.getInteger(R.styleable.AdaptiveActionsToolbar_aat_max_shown, 100); | ||
|
||
array.recycle(); | ||
} | ||
|
||
@Override | ||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
adjustMenuActions(getMenu(), maxShown, getMeasuredWidth()); | ||
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
} | ||
|
||
public static void adjustMenuActions(@NonNull Menu menu, int maxToShow, int toolbarWidthPx) { | ||
int menuSize = menu.size(); | ||
|
||
int widthAllowed = toolbarWidthPx - ViewUtil.spToPx(NAVIGATION_SP); | ||
int nItemsToShow = Math.min(maxToShow, Math.round(widthAllowed / ViewUtil.spToPx(ACTION_VIEW_WIDTH_SP))); | ||
|
||
if (nItemsToShow < menuSize) { | ||
widthAllowed -= ViewUtil.spToPx(OVERFLOW_VIEW_WIDTH_SP); | ||
} | ||
|
||
nItemsToShow = Math.min(maxToShow, Math.round(widthAllowed / ViewUtil.spToPx(ACTION_VIEW_WIDTH_SP))); | ||
|
||
for (int i = 0; i < menu.size(); i++) { | ||
MenuItem item = menu.getItem(i); | ||
if (nItemsToShow > 0) { | ||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | ||
} else { | ||
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); | ||
} | ||
nItemsToShow--; | ||
} | ||
} | ||
|
||
} |
8 changes: 4 additions & 4 deletions
8
app/src/main/res/layout/conversation_reaction_long_press_toolbar.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" | ||
<org.thoughtcrime.securesms.util.views.AdaptiveActionsToolbar xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
android:background="@color/action_mode_status_bar" | ||
android:theme="@style/TextSecure.DarkActionBar.Conversation" | ||
app:navigationIcon="@drawable/ic_x_conversation" | ||
app:menu="@menu/conversation_reactions_long_press_menu"> | ||
app:menu="@menu/conversation_reactions_long_press_menu" | ||
app:navigationIcon="@drawable/ic_x_conversation"> | ||
|
||
</androidx.appcompat.widget.Toolbar> | ||
</org.thoughtcrime.securesms.util.views.AdaptiveActionsToolbar> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters