Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
feat(YouTube - Navigation bar components): Add Hide navigation bar
Browse files Browse the repository at this point in the history
…setting
  • Loading branch information
anddea committed Sep 30, 2024
1 parent ce2d61f commit ac2b858
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public static void hideNavigationLabel(TextView view) {
hideViewUnderCondition(Settings.HIDE_NAVIGATION_LABEL.get(), view);
}

public static void hideNavigationBar(View view) {
hideViewUnderCondition(Settings.HIDE_NAVIGATION_BAR.get(), view);
}

// endregion

// region [Layout switch] patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.libraries.youtube.rendering.ui.pivotbar.PivotBar;

import java.lang.ref.WeakReference;

import app.revanced.integrations.shared.utils.ResourceUtils;
import app.revanced.integrations.shared.utils.Utils;
import app.revanced.integrations.youtube.patches.utils.PlayerTypeHookPatch;
import app.revanced.integrations.youtube.settings.Settings;
import app.revanced.integrations.youtube.shared.PlayerType;
import app.revanced.integrations.youtube.utils.VideoUtils;

@SuppressWarnings("unused")
public class ShortsPatch {
private static final boolean ENABLE_TIME_STAMP = Settings.ENABLE_TIME_STAMP.get();
private static final int META_PANEL_BOTTOM_MARGIN;
private static final double NAVIGATION_BAR_HEIGHT_PERCENTAGE;

static {
final int bottomMargin = validateValue(
Expand All @@ -30,6 +38,15 @@ public class ShortsPatch {
);

META_PANEL_BOTTOM_MARGIN = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) bottomMargin, Utils.getResources().getDisplayMetrics());

final int heightPercentage = validateValue(
Settings.SHORTS_NAVIGATION_BAR_HEIGHT_PERCENTAGE,
0,
100,
"revanced_shorts_navigation_bar_height_percentage_invalid_toast"
);

NAVIGATION_BAR_HEIGHT_PERCENTAGE = heightPercentage / 100d;
}

public static Enum<?> repeat;
Expand Down Expand Up @@ -120,7 +137,8 @@ public static boolean hideShortsSoundButton() {
return Settings.HIDE_SHORTS_SOUND_BUTTON.get();
}

private static final int zeroPaddingDimenId = ResourceUtils.getDimenIdentifier("revanced_zero_padding");
private static final int zeroPaddingDimenId =
ResourceUtils.getDimenIdentifier("revanced_zero_padding");

public static int getShortsSoundButtonDimenId(int dimenId) {
return Settings.HIDE_SHORTS_SOUND_BUTTON.get()
Expand All @@ -140,23 +158,93 @@ public static boolean hideShortsToolBar(boolean original) {
return !Settings.HIDE_SHORTS_TOOLBAR.get() && original;
}

private static WeakReference<View> pivotBarViewRef = new WeakReference<>(null);
public static final Boolean HIDE_SHORTS_NAVIGATION_BAR = Settings.HIDE_SHORTS_NAVIGATION_BAR.get();

public static void setNavigationBar(Object pivotBar) {
if (pivotBar instanceof View pivotBarView) {
pivotBarViewRef = new WeakReference<>(pivotBarView);
/**
* BottomBarContainer is the parent view of {@link PivotBar},
* And can be hidden using {@link View#setVisibility} only when it is initialized.
* <p>
* If it was not hidden with {@link View#setVisibility} when it was initialized,
* it should be hidden with {@link FrameLayout.LayoutParams}.
* <p>
* When Shorts is opened, {@link FrameLayout.LayoutParams} should be changed to 0dp,
* When Shorts is closed, {@link FrameLayout.LayoutParams} should be changed to the original.
*/
private static WeakReference<View> bottomBarContainerRef = new WeakReference<>(null);

private static FrameLayout.LayoutParams originalLayoutParams;
private static final FrameLayout.LayoutParams zeroLayoutParams =
new FrameLayout.LayoutParams(0, 0);

public static void setNavigationBar(View view) {
if (!HIDE_SHORTS_NAVIGATION_BAR) {
return;
}
bottomBarContainerRef = new WeakReference<>(view);
if (!(view.getLayoutParams() instanceof FrameLayout.LayoutParams lp)) {
return;
}
if (originalLayoutParams == null) {
originalLayoutParams = lp;
}
}

public static View hideShortsNavigationBar(View view) {
return Settings.HIDE_SHORTS_NAVIGATION_BAR.get() ? null : view;
public static int overrideNavigationBarHeight(int original) {
return HIDE_SHORTS_NAVIGATION_BAR
? (int) Math.round(original * NAVIGATION_BAR_HEIGHT_PERCENTAGE)
: original;
}

public static void hideShortsNavigationBar() {
final View pivotBarView = pivotBarViewRef.get();
if (pivotBarView != null) {
hideViewUnderCondition(Settings.HIDE_SHORTS_NAVIGATION_BAR.get(), pivotBarView);
private static void setNavigationBarLayoutParams(boolean visible) {
if (!HIDE_SHORTS_NAVIGATION_BAR) {
return;
}
final View navigationBar = bottomBarContainerRef.get();
if (navigationBar == null) {
return;
}
if (!(navigationBar.getLayoutParams() instanceof FrameLayout.LayoutParams lp)) {
return;
}
navigationBar.setLayoutParams(
visible
? originalLayoutParams
: zeroLayoutParams
);
}

/**
* Add a listener to the shorts player overlay View.
* Triggered when a regular video view is attached or detached to Windows.
* <p>
* Here are some reasons to use {@link View.OnAttachStateChangeListener} instead of {@link PlayerType}:
* 1. {@link View.OnAttachStateChangeListener} is triggered before {@link PlayerTypeHookPatch#setPlayerType}.
* 2. {@link PlayerType} can't accurately determine if the user is in the feed or in Shorts.
*
* @param view Shorts player overlay (R.id.reel_watch_player).
*/
public static void onShortsCreate(View view) {
if (!HIDE_SHORTS_NAVIGATION_BAR) {
return;
}

view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
/**
* Shorts opened.
*/
@Override
public void onViewAttachedToWindow(@Nullable View v) {
setNavigationBarLayoutParams(false);
}

/**
* Shorts closed.
*/
@Override
public void onViewDetachedFromWindow(@Nullable View v) {
setNavigationBarLayoutParams(true);
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public class Settings extends BaseSettings {
public static final BooleanSetting MINIPLAYER_HIDE_REWIND_FORWARD = new BooleanSetting("revanced_miniplayer_hide_rewind_forward", FALSE, true, MINIPLAYER_TYPE.availability(MODERN_1));
public static final IntegerSetting MINIPLAYER_OPACITY = new IntegerSetting("revanced_miniplayer_opacity", 100, true, MINIPLAYER_TYPE.availability(MODERN_1));

// PreferenceScreen: General - Navigation buttons
// PreferenceScreen: General - Navigation bar
public static final BooleanSetting ENABLE_NARROW_NAVIGATION_BUTTONS = new BooleanSetting("revanced_enable_narrow_navigation_buttons", FALSE, true);
public static final BooleanSetting HIDE_NAVIGATION_CREATE_BUTTON = new BooleanSetting("revanced_hide_navigation_create_button", TRUE, true);
public static final BooleanSetting HIDE_NAVIGATION_HOME_BUTTON = new BooleanSetting("revanced_hide_navigation_home_button", FALSE, true);
Expand All @@ -178,6 +178,7 @@ public class Settings extends BaseSettings {
public static final BooleanSetting HIDE_NAVIGATION_LABEL = new BooleanSetting("revanced_hide_navigation_label", FALSE, true);
public static final BooleanSetting SWITCH_CREATE_WITH_NOTIFICATIONS_BUTTON = new BooleanSetting("revanced_switch_create_with_notifications_button", TRUE, true, "revanced_switch_create_with_notifications_button_user_dialog_message");
public static final BooleanSetting ENABLE_TRANSLUCENT_NAVIGATION_BAR = new BooleanSetting("revanced_enable_translucent_navigation_bar", FALSE, true);
public static final BooleanSetting HIDE_NAVIGATION_BAR = new BooleanSetting("revanced_hide_navigation_bar", FALSE, true);

// PreferenceScreen: General - Override buttons
public static final BooleanSetting OVERRIDE_VIDEO_DOWNLOAD_BUTTON = new BooleanSetting("revanced_override_video_download_button", FALSE);
Expand Down Expand Up @@ -422,6 +423,7 @@ public class Settings extends BaseSettings {
public static final IntegerSetting META_PANEL_BOTTOM_MARGIN = new IntegerSetting("revanced_shorts_meta_panel_bottom_margin", 32, true, parent(ENABLE_TIME_STAMP));
public static final BooleanSetting HIDE_SHORTS_TOOLBAR = new BooleanSetting("revanced_hide_shorts_toolbar", FALSE, true);
public static final BooleanSetting HIDE_SHORTS_NAVIGATION_BAR = new BooleanSetting("revanced_hide_shorts_navigation_bar", FALSE, true);
public static final IntegerSetting SHORTS_NAVIGATION_BAR_HEIGHT_PERCENTAGE = new IntegerSetting("revanced_shorts_navigation_bar_height_percentage", 45, true, parent(HIDE_SHORTS_NAVIGATION_BAR));
public static final BooleanSetting REPLACE_CHANNEL_HANDLE = new BooleanSetting("revanced_replace_channel_handle", FALSE);

// PreferenceScreen: Swipe controls
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.google.android.libraries.youtube.rendering.ui.pivotbar;

import android.content.Context;
import android.widget.HorizontalScrollView;

public class PivotBar extends HorizontalScrollView {
public PivotBar(Context context) {
super(context);
}
}

0 comments on commit ac2b858

Please sign in to comment.